Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array assignment

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a doubt in array assignments.

int[][] v2=new int[3][2];
int[][] v4=new int[3][2];
v2[0][1]=v4;//this is not compiling fine.

byte[][] b=new byte[2][1];
byte b2[][][][]=new byte[2][3][1][2];
b2[0][1]=b;//this is compiling fine.

How and What are the rules here? Please help me to understand.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java does not directly support multidimensional arrays. It does allow us the functionality of multidimensional arrays by allows arrays of arrays.

That is we can an array in which each element is another array.

When calling a method or assigning a value, you have to make sure that what you acessing in the array is the correct type.

I think your first example was something like this

int[][] v1 = new int[3][2];

int[0][1] is an int so you can't assign it an array reference.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply, Keith.Still confusing......I am not clear.Can anyone please clarify it, in a better way.

Thanks
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well if you make this declaration.

int[][] matrix = new int[2][2];

then what matrix is is an array containing 2 objects.

Each of those objects are also int arrays containing 2 elements each.

The type of matrix[0] is int[] so it's legal to say matrix[0].length.

But the type of matrix[0][0] is int so you can only use it like an int.
 
Fintan Quill
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you count the number of dimensions, (i.e. number of '[]') this should help remove the confusion.

Your first examlpe is trying to assign a 2 dimensional array to an int. However, in your second example you are assigning the 3rd and 4th dimensions (2 dimensions) of b2 to the 2 dimensional array b.

This is explained better in the Sierra & Bates book as there are quite a few questions on this in the SCJP exam. As a short hand, count the dimensions!
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please explain giving anyone example like

v2[0][1]=v4;
b2[0][1]=b;

on the below one

int[][] matrix = new int[2][2];
 
Fintan Quill
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>v2[0][1]=v4;

v2 is a 2-dimensional array
v4 is a 2-dimensional array
v2[0][1] is an integer
therefore, v2[0][1] (an integer) cannot be assigned to v4 (a 2-d array)

>b2[0][1]=b;

b2 is a 4-d array
b is a 2-d array
b2[0][1] is a 2-d array
therefore, b2[0][1] can be assigned to b as they have the same dimensionality (they are both 2-d)
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe we should look at a slightly simpler example:

Here a is a "2D array." Really it's an array where all its elements are arrays. This means that a[0] is a "1D array". So when we create b as a "1D array," we can assign b to a[0]. This same pattern works for "higher dimension arrays" as well.

Layne
 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Keith Lynn:
Java does not directly support multidimensional arrays. It does allow us the functionality of multidimensional arrays by allows arrays of arrays.


That is we can an array in which each element is another array.


Dear Keith ,

I am sort of little confused, but what actually is the difference between a
multidimensional array and an array of arrays or arrays of arrays.



Look at all the sentences which seem true and question them.[/QB]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic