• 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

Array of Arrays ???

 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code taken from a mock exam:

I have some questions regarding a few lines of this code:
1. Line 4
It clones 'a' and put it in an Object 'obj' array.
My understanding of this is that an array of single dimension is created where each element contains an array itself. So the content of each dimension of 'obj' will be something like this:
obj[0]={1,2}
obj[1]={0,1,2}
obj[2]={-1,0,2}
Now, if I were to modify the call in line 4 to this:
Object[][] obj = (Object[][])a.clone();
it would fail!
My explanation is that it failed because I am trying to cast each element of 'a', which is of primitive type, to an Object type; as if I issued the following statement:
obj[0][0]=1;
obj[0][1]=2;
:
Am I right on this assumption?
2. Lines 6 & 7
This one I cannot understand.
The way I see it, each element of 'obj' is assigned to a new array 'ia', similar to this one:
ia[0]=obj[0]; // {1,2}
ia[1]=obj[1]; // {0,1,2}
ia[2]=obj[2]; // {-1,0,2}
Now how can line 7 print the basic elements of 'a' by just using a single dimension of 'ia'?
Tks.
Al
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
regarding the first assumption its correct.
if you take 2D array if any object instead of int then casting to Object[][] is allowed at line 4.

on first iteration with i=0
ia = obj[0] // {1,2}
on next iteration with i=1
ia = obj[1] // {0,1,2}
on next iteration with i=2
ia = obj[2] // {-1,0,2}
so its a 1D array only and we can surly print elements of it with single index
pinky
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi pinky,

Originally posted by pinky yadav:
on first iteration with i=0
ia = obj[0] // {1,2}
on next iteration with i=1
ia = obj[1] // {0,1,2}
on next iteration with i=2
ia = obj[2] // {-1,0,2}
pinky



Ok, I got it. I now see the difference.
This line:
int[] ia = (int[])obj[i];
is equivalen to
ia=obj[0];
:
and NOT
ia[0]=obj[0];
:
Tks.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ June 10, 2003: Message edited by: Marlene Miller ]
 
Space seems cool in the movies, but once you get out there, it is super boring. Now for a fascinating tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic