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

时间:2014-03-10 00:51:35

标签: 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 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));
    }
}

问题 - 在这里,我想要做的就是返回一个将使用方法IntSequence subSequence(int index, int size)创建的subArray。但是,当我运行它时我的代码正在做什么是返回以下输出 -

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: 5
Multiplying subarray's last element
Subarray after modification:
s[0]: 20
s[1]: 300
s[2]: 20
s[3]: 30
s[4]: 40
Array after modification:
a[0]: 20
a[1]: 300
a[2]: 20
a[3]: 30
a[4]: 40
First element changed to: -10

然而,这是预期的输出 -

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

ISSUE - 从上面的预期输出中可以看出,当我得到一个子数组时,应该更新索引 - 索引处的原始数组(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;

我知道这可能是制作提供预期输出的代码的最糟糕方式,但我不允许制作一个直接具有整数数组返回类型的方法以达到目的。我一直试图找出一种方法来完成几天的需要。

非常感谢你花时间去看它!

2 个答案:

答案 0 :(得分:0)

为了实施

  

也会影响原始序列。

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

工作原理:

这是关于java中对象与原始类型的引用。在原始类型的情况下,a = b实际上在a中创建b的COPY,在对象a = b的情况下,仅将对象b的引用复制到a中。因此,改变b的内部结构会影响你所看到的但是参考a。

答案 1 :(得分:0)

看看如何:

public IntArray(int index, int size, int[] array)

正在构建其内部数组。

返回的subSequence是否包含整个原始序列?