| Author |
Cloneable Object
|
deepu Bhalotia
Ranch Hand
Joined: Apr 19, 2005
Posts: 39
|
|
class HelloExam { public static void main(String[] args) { int[][] a = {{1,2},{0,1,2},{-1,0,2}}; // 1 Object[] obj = (Object[])a.clone(); // 2 for(int i = 0; i < obj.length; i++) // 3 { int[] ia = (int[])obj[i]; // 4 System.out.print(ia[i]); // 5 } } } This Question is given in http://www.danchisholm.net/july21/mybook/chapter5/exam1ans.html. Answer of this Question is 112. But i am not able to understand. Can Anybody help me in this matter. [ April 27, 2005: Message edited by: deepak bhalotia ]
|
 |
vinuharan haran
Ranch Hand
Joined: Feb 26, 2005
Posts: 64
|
|
Hi, Clone method creates a copy of the object.so,object 'a' is cloned and is stored in 'obj' array.Now length of 'obj' array is 3.Let us go through the loop now. when i=0, ia=obj[0] so ia,obj[0] refer to a[0]. hence ia[0] is same as a[0][0] which is 1. when i=1 ia=obj[1] ia,obj[1] refer to a[1] so ia[1] is same as a[1][1] which is 1 again. when i=2 ia=obj[2] ia,obj[2] refer to a[2] so ia[2] is same as a[2][2] which is 2 Hope this helps...
|
 |
deepu Bhalotia
Ranch Hand
Joined: Apr 19, 2005
Posts: 39
|
|
Hi Haran Can you explain how the array length is 3? We are making a clone of two dimensional array into one dimensional array. Thanks Deepak
|
 |
vinuharan haran
Ranch Hand
Joined: Feb 26, 2005
Posts: 64
|
|
Hi, The length of array a is 3.so obj.length is also 3.That is 'a' has three elements. Dimensional arrays are considered as array of arrays. Now, a has three elements which are references to other array.So a.length is 3. a[0] is again a array of 2 elements .so,length of a[0] is 2. length of a[1] and a[2] is 3. Hope this helps...
|
 |
deepu Bhalotia
Ranch Hand
Joined: Apr 19, 2005
Posts: 39
|
|
Thanks Vinu.... It really Worked.... Thanks, Deepak
|
 |
 |
|
|
subject: Cloneable Object
|
|
|