File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array reference assignment

 
Shiva Mohan
Ranch Hand
Posts: 484
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
byte[] b1=new byte[8];
byte b2[][][][]=new byte[2][3][1][2];

b2[0][0][0]=b1;//------------------compiling fine by compiler

b2 is three dimensional array,b1 is one dimensional array. How come one dimensional array is assigned to three dimensional arrays? Please Help me.
 
Henry Wong
author
Marshal
Pie
Posts: 20828
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shiva Mohan:
byte[] b1=new byte[8];
byte b2[][][][]=new byte[2][3][1][2];

b2[0][0][0]=b1;//------------------compiling fine by compiler

b2 is three dimensional array,b1 is one dimensional array. How come one dimensional array is assigned to three dimensional arrays? Please Help me.


b1 is an array of bytes

b2 is an array of arrays of arrays of arrays of bytes

b2[0] is the first array of arrays of arrays of bytes in the b2 array

b2[0][0] is the first array of arrays of bytes the b2[0] array

b2[0][0][0] is the first array of bytes in the b2[0][0] array


The assignment is legal because both b1 and b2[0][0][0] are references for an array of bytes.

Henry
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shiva Mohan:
byte[] b1=new byte[8];
byte b2[][][][]=new byte[2][3][1][2];

b2[0][0][0]=b1;//------------------compiling fine by compiler

b2 is three dimensional array,b1 is one dimensional array. How come one dimensional array is assigned to three dimensional arrays? Please Help me.


b2 is a reference to an array of an array of an array of an array of bytes

So b2[0][0][0] is a byte array.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats strange ! I replied here but my post seems to have disappeared. I ll do it again anyway...

The byte b2 is a four dimensional array and when you say b2[0][0][0] it has one dimension left, and you can assign a one dimensional array to that reference. Consider this...

byte b1[] = { 1,2 };
byte b2[][] = new byte[2][2];
b2[0] = b1;
System.out.println( b2[0][0] + " " + b2[0][1] );

This code will print 1 2
 
Shiva Mohan
Ranch Hand
Posts: 484
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Henry,Keith,John.But, it still confusing.....

b2[0][0]=b1;------------(1)

this one is not compiling.My analysis is b2[0][0] is a two dimensional array,b1 is one dimensional array.Assigning one dimensional array to two dimansinal array is invalid.I applied that same concept to
b2[0][0][0]=b1;But,here it is not working.Can anyone explain it in the same way that i understood like 1.
 
Shiva Mohan
Ranch Hand
Posts: 484
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought b2[0][0][0] is a three dimensinal byte array.But ,you guys are saying b2[0][0][0] is a one dimensional byte array.That part,i don't get.Please help me to get understand.
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have the declaration byte[][][] b, then b is a reference to the Java equivalent of a 3-D array of bytes.

However, in your example, you have an array where you have four dimensions b[][][][].

If you access b[0][0][0], then because of how you have defined b, this is a 1-D array of bytes.

b[0][0][0][0] is a byte.
 
Shiva Mohan
Ranch Hand
Posts: 484
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again Keith.I got it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic