• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Cloneable Object

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Vinu....

It really Worked....

Thanks,
Deepak
 
He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic