简体   繁体   中英

Replacing the values in the original array to the new one

I have got an 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);
}

And a class that implements it -

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));
    }
}

PROBLEM - Here, all I want to do is return a subArray which would be created using the method IntSequence subSequence(int index, int size) . However, what my code is doing when I run it is return the following output -

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

Whereas, this is the expected output -

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 - As you can see from the expected output above, when I get a sub-array, the values of the original array (a) at index - index should be updated aswell to the value cast to the sub-array.

For example -

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;

I know this is probably the worst way of making a code that gives the expected output but I am not allowed to make a method which would straight away have integer array return type in order to achieve the purpose. I have been trying to figure out a way to do the needful for days now.

Thanks a lot for taking your time to look at it!

In order to implement

affects the original sequence as well.

part which is the most challenging you'd need to wrap your integers into Objects and store array or list of those wrapper objects in your implementation. This way changing internal int in this wrappers will affect all your sequences since by storing wrappers you will only store references to those wrapper objects and not objects themselves as in case of int.

How it works:

This is all about reference to an object vs primitive types in java. In case of primitive types a = b actually create a COPY of b in a, in case of objects a = b copies only reference to the object b into a. Therefore changing b's internals will affect what you'll see though reference a.

Look at how:

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

is constructing its internal array.

Is the subSequence returned meant to contain the whole of the original sequence?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM