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,]
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,]