posted 15 years ago
Here I have added extra displays to show what 'a' and 'obj' really look like inside....they are very similar.
So obj is really an array of 3 arrays.
The for loop executes 3 times.
Line 4 serves up the 1st, 2nd, + 3rd array from inside of obj.
Line 5 serves up 1st, 2nd, + 3rd value from the array produced in line 4.
Does this help?
int[][] a = {{1,2},{0,1,2},{-1,0,2}}; // 1
System.out.println("a = " + a.length); // 1.5
System.out.println("a = " + Arrays.toString(a[0])); // 1.6
System.out.println("a = " + Arrays.toString(a[1])); // 1.7
System.out.println("a = " + Arrays.toString(a[2])); // 1.8
Object[] obj = (Object[])a.clone(); // 2
System.out.println("obj = " + obj.length); // 2.5
System.out.println("obj = " + Arrays.toString((int[])obj[0])); // 2.6
System.out.println("obj = " + Arrays.toString((int[])obj[1])); // 2.7
System.out.println("obj = " + Arrays.toString((int[])obj[2])); // 2.8
for(int i = 0; i < obj.length; i++) { // 3
int[] ia = (int[])obj[i]; // 4
System.out.print(ia[i]); // 5
}
RESULT:
a = 3
a = [1, 2]
a = [0, 1, 2]
a = [-1, 0, 2]
obj = 3
obj = [1, 2]
obj = [0, 1, 2]
obj = [-1, 0, 2]
112
[ December 06, 2008: Message edited by: patrick avery ]
[ December 06, 2008: Message edited by: patrick avery ]
SCJA 96%
SCJP 6 88%
skipping SCJD to work on passing SCWCD