Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将原始数组中的值替换为新值_Java_Arrays_Interface - Fatal编程技术网

Java 将原始数组中的值替换为新值

Java 将原始数组中的值替换为新值,java,arrays,interface,Java,Arrays,Interface,我有一个接口- //INTERFACE - public interface IntSequence { int length(); int get(int index); void set(int index, int value); /** * Returns a contiguous subsequence of size "size" which starts from * the index "index" and is backed

我有一个接口-

//INTERFACE -
public interface IntSequence {
    int length();
    int get(int index);
    void set(int index, int value);
    /**
     * Returns a contiguous subsequence of size "size" which starts from
     * the index "index" and is backed by the sequence;
     * that is, changing it through {@link IntSequence#set(int, int)}
     * affects the original sequence as well.
     * @param index the starting position of the subsequence
     * @param size the subsequence size
     * @return a sequence of ints
     */
    IntSequence subSequence(int index, int size);
}
以及一个实现它的类-

public class IntArray implements IntSequence {

    int[] a;
    static int test;
    static int[] b;
    static int[] c;
    int[] d;
    int use;
    int j;
    int[] mama;
    int[] mama2;
    int indexgeter;

    public IntArray(int size) {
        j = size;
        a = new int[size];
        b = new int[size];
        a = b;
    }

    public IntArray(int index, int size, int[] array) {
        this.a = array;
        int counter = 0; 
        while(counter < size) {
            array[counter] = array[index];
            counter++;
            index++;
        }
    }

    @Override
    public int length() {
        return a.length;
    }

    @Override
    public int get(int index) {
        return a[index];
    }

    @Override
    public void set(int index, int value) {
        a[index] = value;
    }

    @Override
    public IntSequence subSequence(int index, int size) {
        IntSequence resultseq = new IntArray(index, size, a);
        return resultseq;
    }
    public static void main(String[] args) {
        IntSequence a = new IntArray(5);
        a.set(0, 0);
        a.set(1, 10);
        a.set(2, 20);
        a.set(3, 30);
        a.set(4, 40);

        System.out.println("Initial array");
        System.out.println("size: " + a.length());
        for (int i = 0; i < 5; ++i)
            System.out.println("a[" + i + "]: " + a.get(i));

        System.out.println("Creating subarray (2, 2)");
        IntSequence s = a.subSequence(2, 2);
        System.out.println("s.size: " + s.length());

        System.out.println("Multiplying subarray's last element");
        s.set(1, s.get(1) * 10);
        System.out.println("Subarray after modification:");
        for (int i = 0; i < s.length(); ++i)
            System.out.println("s[" + i + "]: " + s.get(i));

        System.out.println("Array after modification:");
        for (int i = 0; i < 5; ++i)
            System.out.println("a[" + i + "]: " + a.get(i));

        a.subSequence(0, 1).subSequence(0, 1).subSequence(0, 1).set(0, -10);
        System.out.println("First element changed to: " + a.get(0));
    }
}
然而,这是预期的输出-

Initial array
size: 5
a[0]: 0
a[1]: 10
a[2]: 20
a[3]: 30
a[4]: 40
Creating subarray (2, 2)
s.size: 2
Multiplying subarray's last element
Subarray after modification:
s[0]: 20
s[1]: 300
Array after modification:
a[0]: 0
a[1]: 10
a[2]: 20
a[3]: 300
a[4]: 40
First element changed to: -10
问题正如您从上面的预期输出中所看到的,当我获得子数组时,index-index处的原始数组(a)的值也应更新为转换到子数组的值

例如—

int[] a = new int[] {1,2,3,4,5}; //we have this original array over here
//You use subSequence on it
a.subSequence(2,2);
//Now the elements you will have will be
s[0] = a[2]; //Which will be 3 in this case
s[1] = a[3]; //Which will be 4 in this case

//You make some changes on s[0]
s[0] * 10;
s[1] * 100;

//The original array whose indexes s referred to should be modified aswell now cause you did some changes to the subarray
//The new array will be
a[0] = 1;
a[1] = 2;
a[2] = 30;
a[3] = 400;
a[4] = 5;
我知道这可能是生成给出预期输出的代码的最糟糕的方法,但我不允许生成一个直接使用整数数组返回类型的方法来实现此目的。几天来,我一直在想办法做必要的事

非常感谢您抽出时间来查看它

为了实施

也会影响原始序列

最具挑战性的部分是将整数包装到对象中,并在实现中存储这些包装器对象的数组或列表。这种方式更改此包装中的内部int将影响所有序列,因为通过存储包装,您将只存储对这些包装对象的引用,而不会像int那样存储对象本身

工作原理:

这都是关于在java中对对象和基本类型的引用。对于基本类型a=b,实际上在a中创建b的副本;对于对象a=b,仅将对对象b的引用复制到a中。因此,更改b的内部结构将影响您通过参考a看到的内容。

看看如何:

public IntArray(int index, int size, int[] array)
正在构造其内部数组


返回的子序列是否意味着包含整个原始序列?

否作为子序列状态的描述-返回大小为“size”的连续子序列,该子序列从索引“index”开始,并由序列支持;描述中指出,子序列由序列支持,这对我来说,子序列确实需要对原始序列的引用,但只需要知道其操作的原始序列的范围。在你的例子中,索引2和索引3。对不起,我把你的问题弄糊涂了。是的,你在上面所说的是对的。我可能措辞不好,已经晚了:)但你真的应该看看你是如何构建序列的,它可能对你的初始序列有效,但我不确定它对你的子序列有效。看看阿泰姆的回答。
public IntArray(int index, int size, int[] array)