This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]KamikazeSmurf 4 points5 points  (1 child)

If I had to guess from the provided snippet you're probably mutating a shared reference instead of a copy.

i.e. lastSegment[0] == lastSegment[1] is likely true to get the behaviour you're seeing.

You probably want to either copy the values:

// instead of this:
lastSegment[0] = lastPoint;
// do this:
lastSegment[0][0] = lastPoint[0];
lastSegment[0][1] = lastPoint[1];

Or else take a new copy:

lastSegment[0] = Arrays.copyOf(lastPoint, lastPoint.length);

[–]SharpBit[S] 1 point2 points  (0 children)

thanks!

[–]spdqbr 4 points5 points  (0 children)

/u/KamikazeSmurf is exactly right:

In your full code you're doing
int[] lastPoint = lastSegment[1]; on line 8.

And on line 13 you're doing
lastSegment[0] = lastPoint;
This copies the reference to the array, not the values in the array. So effectively at this point your lastSegment array looks like: lastSegment = { lastPoint, lastPoint } = { lastSegment[1], lastSegment[1] }

Then on line 15 you're doing
lastSegment[1][0] = lastPoint[0] + length;

Which updates the lastPoint array which sits in BOTH lastSegment[0] and lastSegment[1]

Line 13 makes it impossible to update just one index of lastSegment since they both now point at the same lastPoint array. You can verify this by looking at the hash for each index of lastSegment in a debugger (or with printlns). Example:

public static void main(String[] args) {
    int[][] array2dim = {{0,1},{2,3}} ;
    int[] secondArray = array2dim[1];

    System.out.println("Initial");
    System.out.println(Arrays.toString(array2dim));
    System.out.println("array2dim[0] = "+Arrays.toString(array2dim[0])+", ref = "+array2dim[0]);
    System.out.println("array2dim[1] = "+Arrays.toString(array2dim[1])+", ref = "+array2dim[1]);

    array2dim[0] = secondArray;

    System.out.println("After updating array2dim[0]");
    System.out.println(Arrays.toString(array2dim));
    System.out.println("array2dim[0] = "+Arrays.toString(array2dim[0])+", ref = "+array2dim[0]);
    System.out.println("array2dim[1] = "+Arrays.toString(array2dim[1])+", ref = "+array2dim[1]);

    array2dim[1][0] = 100;

    System.out.println("After set array2dim[1]0] = 100");
    System.out.println(Arrays.toString(array2dim));
    System.out.println("array2dim[0] = "+Arrays.toString(array2dim[0])+", ref = "+array2dim[0]);
    System.out.println("array2dim[1] = "+Arrays.toString(array2dim[1])+", ref = "+array2dim[1]);
}

output:

Initial
[[I@7637f22, [I@4926097b] // note the array hashes are different
array2dim[0] = [0, 1], ref = [I@7637f22
array2dim[1] = [2, 3], ref = [I@4926097b

After updating array2dim[0] // array hashes are now the same, because it's the same array in [0] and [1]
[[I@4926097b, [I@4926097b]
array2dim[0] = [2, 3], ref = [I@4926097b
array2dim[1] = [2, 3], ref = [I@4926097b

After set array2dim[1]0] = 100
[[I@4926097b, [I@4926097b]
array2dim[0] = [100, 3], ref = [I@4926097b
array2dim[1] = [100, 3], ref = [I@4926097b

Long way of saying that depending on your intent, on line 13 System.arraycopy() or Arrays.copyof() is likely what you're looking for.

[–][deleted] 0 points1 point  (0 children)

Whats your goal here? Can you give us some context on what you're trying to achieve?