Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Array assignments

Thread view: 
Mike - 06 Jun 2006 10:12 GMT
Hello Newsgroup,

i am just fighting with arrays ...

Given the following:
byte [][] b1 = new byte [10][10];
byte [][] b2 = new byte [2][1];
byte big [][][][] = new byte [2][3][1][2];

Why does this work?
big[0][1] = b1;
// or this one
b2 = b1;

And why does this not work?
big[1][2][0] = b2;
// or this one
big[1] = b2;

Compiler complains in both situations:
Type mismatch: cannot convert from byte[][] to byte[]
Type mismatch: cannot convert from byte[][] to byte[][][]

Thank you in advance
mike
Alex Hunsley - 06 Jun 2006 10:57 GMT
> Hello Newsgroup,
>
[quoted text clipped - 7 lines]
> Why does this work?
> big[0][1] = b1;

The type of big is byte[][][][].

If you write big[x], where x is a number,
the type of that is going to be byte[][][]
(because you've already indexed into one part of this array that you
could think of as multidimensional).

Similarly, the type of big[x][y] (x and y are numbers) is byte[][].

So, when you write:

  big[0][1] = b1;

this is allowed because the *type* of the items on both sides is byte[][].

> // or this one
> b2 = b1;

Similarly, the types of b2 and b1 are boths byte[][]. The types match.
Therefore you can do this assignment.

> And why does this not work?
> big[1][2][0] = b2;

The type of the left hand side expression is byte[].
The type of the right hand side is byte[][].
You can't assign these, it doesn't make sense to, and the Java compiler
spots that.

> // or this one
> big[1] = b2;

Type of LHS is byte[][][]; type of RHS is byte[][]. These are not the
matching types.

> Compiler complains in both situations:
> Type mismatch: cannot convert from byte[][] to byte[]
> Type mismatch: cannot convert from byte[][] to byte[][][]

And it was right to complain! Perhaps what you are wanting to do is
manually copy the data from one multidimensional array into the other.

It helps to think of these multidimensional arrays as arrays of arrays.

For example, byte[] is an array of bytes - easy enough.
For byte[][], think of having an array, where each array member is a
byte[] - an array of bytes!

lex
Mike - 06 Jun 2006 17:41 GMT
Hi guys,
Thank you for the answers.
Mike

>> Hello Newsgroup,
>>
[quoted text clipped - 57 lines]
>
> lex
Lee Weiner - 06 Jun 2006 14:29 GMT
>byte [][] b1 = new byte [10][10];
>byte [][] b2 = new byte [2][1];
[quoted text clipped - 9 lines]
>// or this one
>big[1] = b2;

Think of it this way:  Each element of big[] IS a 3-dimension array.  Each
element of big[][] IS a 2-dimension array.  Each element of big[][][] IS a
1-dimension array.  Do the results make sense now?

Lee Weiner
lee AT leeweiner DOT org


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.