2

I want to save all the int[] data in my array list so i can see every thing step by step. Only my problem is that it overrides the already existing int[] in my ArrayList. how can i fill my array list without overriding my old int in the ArrayList?

ArrayList<int[]> lijstje = new ArrayList<int[]>();
    public int[] data = {7,4,8,56,67,85,23,65,23,65,23,22};
int stemp;
int len = 10;
public void shellSort(){
        while (h <= len / 3) {
            h = h * 3 + 1;
        }
        while (h > 0) {

            for (outer = h; outer < len; outer++) {
                stemp = data[outer];
                inner = outer;

                while (inner > h - 1 && data[inner - h] >= stemp) {
                    data[inner] = data[inner - h];
                    inner -= h;
                }
                data[inner] = stemp;
                lijstje.add(data);
            }
            h = (h - 1) / 3;
        }
    }

1 Answer 1

3

Arrays are stored as references, so when you change the array one place, anywhere else you directly stored it will change to. Instead, make a brand new array with the same values, and store that. To do that, do array.clone(), so for you

ArrayList<int[]> lijstje = new ArrayList<int[]>();
public int[] data = {7,4,8,56,67,85,23,65,23,65,23,22};
int stemp;
int len = 10;
public void shellSort(){
    while (h <= len / 3) {
        h = h * 3 + 1;
    }
    while (h > 0) {

        for (outer = h; outer < len; outer++) {
            stemp = data[outer];
            inner = outer;

            while (inner > h - 1 && data[inner - h] >= stemp) {
                data[inner] = data[inner - h];
                inner -= h;
            }
            data[inner] = stemp;
            lijstje.add(data.clone()); // Notice here how it's data.clone() instead of just data
        }
        h = (h - 1) / 3;
    }
}

Here's an example showing how arrays are passed by referencing, this

int[] original = { 1, 2, 3 };
int[] passedByReference = original;
int[] cloned = original.clone();
System.out.println("Before:");
System.out.println(Arrays.toString(original));
System.out.println(Arrays.toString(passedByReference));
System.out.println(Arrays.toString(cloned));
original[0]=10;
System.out.println("After:");
System.out.println(Arrays.toString(original));
System.out.println(Arrays.toString(passedByReference));
System.out.println(Arrays.toString(cloned));

will have the following output

Before:
[1, 2, 3]
[1, 2, 3]
[1, 2, 3]
After:
[10, 2, 3]
[10, 2, 3]
[1, 2, 3]

as you can see, the cloned one is not affected, whereas the original and passed-by-reference ones are. In your code, you don't want changes to the original to affect the array you store, so you must clone it some way (array.clone() is a nice simple way for a 2D array).

Sign up to request clarification or add additional context in comments.

1 Comment

@Henk Yup, happy to help! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.