• 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
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

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 ]
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic