• 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 and Array Reference Question

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,
I am preparing for the SCJP exam. I have a doubt, please clarify it for me.


public class Java_arrays {

public static void main(String[] args){
byte[][] big=new byte[7][7];
byte[][] b=new byte[2][1];
byte b3=45;
byte b2[][][][]=new byte[2][3][1][2];

}
}


Which of the code should be inserted after the code to compile correctly?

1) If I insert the code b2[0][1]=b; // it works and I got it.

2) If I insert the code b2[1][1][0]=b[0][0];
//because two-dimensional array cannot fit in three dimensional specifications (I understood like this)


3) If I insert the code b2[0][1][0][0]=b[0][0]; //compiler accepts it.


4) If I insert the code b2[1][2][0][1]=b; //Compiler won�t accept this

I don�t get this 3 and 4.Can anyone please help me clarify this.I am looking for the rules here,please.

Thanks.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually, in the case 2) you don't have a two-dimensional array. b[0][0] is a byte, while b2[1][1][0] is an array of bytes.

So, the whole concept does not deal with "multidimensional arrays" but rather with "arrays of arrays" and "arrays of arrays of arrays"

There is not a multidimensional memory allocated, think of it as a tree.

So for example, b[0] is an array of bytes, and also b[1].
Both can hold an array of arbitrary length (even null), e.g.
b[0] = new byte[123];
b[1] = new byte[456];
would be perfectly legal.

Greetings
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


3) If I insert the code b2[0][1][0][0]=b[0][0]; //compiler accepts it.
4) If I insert the code b2[1][2][0][1]=b; //Compiler won�t accept this



For 3) b2[0][1][0][0]=b[0][0];
b2 is a 4D array of bytes...eventually the b2[x][x][x][x] (the last dimension's element) should have a byte.

Similarly b is a 2D array of bytes...so b[x][x] (again the last dimensions's element) is a byte.

hence the assignment b2[0][1][0][0]=b[0][0] (byte = anotherbyte) is accepted.

For 4) b2[1][2][0][1]=b
As said before b2 is a 4D array of bytes...eventually the b2[x][x][x][x] (the last dimension's element) should have a byte.

And b is on the whole a 2D array hence it cannot be directly assiged to a single byte location.

b2[1][2][0][1]=b (byte = 2D Array) -- is not accepted!!

Hope this was helpful!!

Mel
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.I got it.
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hii ,

i've some problem in understanding the concept of array of arrays of arrays and so on ... so i sort of developed my own way of finding the solution ...

i check the dimension of array on the L.H.S and see if the R.H.S variable completes its dimension or not .... if it doesn't then assignment cannot be done..

i was just wondering whether it is right to do this way or not ... if not plz help me with a simple explanation of this topic.

thanks

ashi
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is the easiest and the best method
In fact I use the same method
But just make sure that neither LHS nor the RHS result in a
NullPointerException or ArrayIndexOutOfBoundsException

Good luck,
santhosh sharma (scjp 1.4 100%)
 
Asha Pathik
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Santosh for clearing my doubt....

ashi
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic