This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
at line 2, you are creating a clone of the array. then you assign that clone to an Object. As you might know, that int and every other primitive array is a sub-class of Object. But since this is a 2-D primitive array, so you are able to assign to an Object. You can think of a 1-D int array as an Object. So a 2-d int array as an array of 1-d int array. This is why the assignment is allowed...
ok but which values can be assigned to 1-d Object array?
1-d object array can store any type of an array except 1-d primitive arrays.
how values in 2-d array can b converted in to 1-d array?
values in 2-d array cannot be converted into 1-d array (at least directly without using any loop or something). I think you are confused here. The 2-d array converted into a 1-d array because we changed the type. We did not assign a 2-d int array into 1-d int array. We assigned a 2-d int array into a 1-d object array...
Originally posted by meera kanekal: Patrick and Ankit I found your explanation very helpful. But I don't understand why the output is 112. Could you please explain where that is coming from? Thanks, Meera
Because, i doesn't remain constant. It changes from 0 to 1 to 2 with each iteration of the for loop. Therefore, obj = 1, obj = 1 and obj = 2.
Does this make sense?
SCJP 1.4 85%
Joined: Feb 13, 2005
So Prateek am I correct in saying that 1 --> 0th element of ia = [1,2] 1 --> 1st element of ia = [0,1,2] 2 --> 2nd element of ia = [-1,0,2] Thanks, Meera
Joined: Feb 14, 2006
Yes. Basically, obj[i] contains the same element as ia[i] except for the fact that it needs to be cast to an int.