aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array reference assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array reference assignment" Watch "Array reference assignment" New topic
Author

Array reference assignment

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
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
Sheriff

Joined: Sep 28, 2004
Posts: 18133
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Feb 24, 2006
Posts: 6657
    
    5

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


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
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

Joined: Jan 05, 2006
Posts: 477
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Jan 05, 2006
Posts: 477
Thanks again Keith.I got it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array reference assignment
 
Similar Threads
array assignments
Arrays
Array Reference Assignments
Multidimensional Array Assignments
Regarding assiging a reference to array object