|
Arrays
Nov 17, 2001 17:27:40 GMT -5
Post by Sylph on Nov 17, 2001 17:27:40 GMT -5
how is the arrays equals method supposed to work? int[][] array1 = new int[4][2]; for (int i = 0; i < 4; i++){ array1 [0] = i; array1[1] = 0; } int[][] array2 = new int[4][2]; array2 = array1; array2[0][0] = 991; System.out.println(Arrays.equals(array1[0], array2[0]));
when i print this, it prints true. what's wrong?
|
|
|
Arrays
Nov 18, 2001 9:24:01 GMT -5
Post by Yingster on Nov 18, 2001 9:24:01 GMT -5
cuz u set array2=array1
|
|
|
Arrays
Nov 18, 2001 13:42:04 GMT -5
Post by Sylph on Nov 18, 2001 13:42:04 GMT -5
//how come this doesn't work? int[][] array1 = new int[4][2]; int[][] array2 = new int[4][2]; array1[3][0] = 2; array1[3][1] = 7; array2[3][0] = 2; array2[3][1] = 7; System.out.println(array1[3][0]); System.out.println(array1[3][1]); System.out.println(array2[3][0]); System.out.println(array2[3][1]); System.out.println(array1[3].equals(array2[3])); //this prints false
|
|
|
Arrays
Nov 18, 2001 14:01:24 GMT -5
Post by Yingster on Nov 18, 2001 14:01:24 GMT -5
u either compare an element in the array or the entire array, i dont think u can compare a row in the array
|
|
|
Arrays
Nov 18, 2001 19:37:35 GMT -5
Post by Majin_Blues on Nov 18, 2001 19:37:35 GMT -5
it depends on your equals method... (inside, did you use "==" for the entire row? if you did, see below)
from your code it seems that each row is pointing to a different area in memory... though the values inside may be equal, they're still pointing to different sections of the memory, which is why it's false
|
|