Strange System.arraycopy behaviour

P

Peter

Hi,

I really can't figure this out and it is quite upsetting that i am not
able to solve a apparently very simple problem. But after shifting de
elements in an array one position to the left using System.arraycopy,
the last and 2nd last element in the array is changed when re-setting
the value of the last element. Something tells me that the last and
2nd last element do point to the same instance, but do not get this
simple issue solved.

I added the method and output to demonstate what my problem is. I
expected the output

Column:0[null,null,]
Column:1[null,null,]
Column:2[null,null,]
Column:3[null,25,]
Column:4[null,24,]




public void arraytest() {

Integer[][] data = new Integer[5][2] ;

System.out.println("Initialized values") ;
for (int a=0 ; a < data.length ; a++ ) {
System.out.print("Column:" + a + "[") ;
for (int b=0 ; b < data[a].length ; b++ ) {
System.out.print(data[a] + ",") ;
}
System.out.println("]") ;
}

data[4][1] = 25 ;
System.out.println("Changed one value") ;
for (int a=0 ; a < data.length ; a++ ) {
System.out.print("Column:" + a + "[") ;
for (int b=0 ; b < data[a].length ; b++ ) {
System.out.print(data[a] + ",") ;
}
System.out.println("]") ;
}

int numelmts = data.length-1 ;
System.arraycopy(data , 1 , data , 0 , numelmts) ;

System.out.println("Shifted all values one position to the left") ;
for (int a=0 ; a < data.length ; a++ ) {
System.out.print("Column:" + a + "[") ;
for (int b=0 ; b < data[a].length ; b++ ) {
System.out.print(data[a] + ",") ;
}
System.out.println("]") ;
}

data[4][1] = 24 ;

System.out.println("Changed same column/row value") ;
for (int a=0 ; a < data.length ; a++ ) {
System.out.print("Column:" + a + "[") ;
for (int b=0 ; b < data[a].length ; b++ ) {
System.out.print(data[a] + ",") ;
}
System.out.println("]") ;
}


}




Initialized values
Column:0[null,null,]
Column:1[null,null,]
Column:2[null,null,]
Column:3[null,null,]
Column:4[null,null,]
Changed one value
Column:0[null,null,]
Column:1[null,null,]
Column:2[null,null,]
Column:3[null,null,]
Column:4[null,25,]
Shifted all values one position to the left
Column:0[null,null,]
Column:1[null,null,]
Column:2[null,null,]
Column:3[null,25,]
Column:4[null,25,]
Changed same column/row value
Column:0[null,null,]
Column:1[null,null,]
Column:2[null,null,]
Column:3[null,24,]
Column:4[null,24,]
 
E

Eric Sosman

Hi,

I really can't figure this out and it is quite upsetting that i am not
able to solve a apparently very simple problem. But after shifting de
elements in an array one position to the left using System.arraycopy,
the last and 2nd last element in the array is changed when re-setting
the value of the last element. Something tells me that the last and
2nd last element do point to the same instance, but do not get this
simple issue solved.

You're on the right track, but you haven't gone quite far
enough. Remember that a two-dimensional array in Java is really
a one-dimensional array containing references to one-D arrays:

Integer[][] data = new Integer[5][2];

.... is quite similar to

Integer[] d0 = new Integer[] { null, null };
Integer[] d1 = new Integer[] { null, null };
Integer[] d2 = new Integer[] { null, null };
Integer[] d3 = new Integer[] { null, null };
Integer[] d4 = new Integer[] { null, null };

Integer[][] data = { d0, d1, d2, d3, d4 };

.... the main difference being that we now have names for the
lower-level arrays. With me so far?

Your code now sets data[4][1] = 26, which is the same as
setting d4[1] = 25. Everything looks the same as above, except
that the second null in d4 is replaced by a reference to an Integer.
Then you use

System.arrayCopy(data, 1, data, 0, data.length - 1);

.... and here's where I think your understanding begins to falter.
After this, the contents of the outer array, data, are

{ d1, d2, d3, d4, // shifted over by arrayCopy()
d4 } // undisturbed

Note that the five elements of data are references to just four
arrays, with two references to the d4 array. Both of data[3] and
data[4] are references to d4: There's one single d4 array with
two references pointing at it:

data[0] -> { null, null }

data[1] -> { null, null }

data[2] -> { null, null }

data[3] -+
+-> { null, 25 }
data[4] -+

Now I hope you can see what's happening. When you assign
data[4][1] = 24 it's the same as d4[1] = 24. You could have
achieved the same result with data[3][1] = 24, because both
data[3] and data[4] refer to the same d4 array.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top