| Author |
Array assignment
|
Shiva Mohan
Ranch Hand
Joined: Jan 05, 2006
Posts: 465
|
|
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
Joined: Feb 07, 2005
Posts: 2341
|
|
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
Joined: Jan 05, 2006
Posts: 465
|
|
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
Joined: Feb 07, 2005
Posts: 2341
|
|
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
Joined: Feb 28, 2006
Posts: 8
|
|
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
Joined: Jan 05, 2006
Posts: 465
|
|
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
Joined: Feb 28, 2006
Posts: 8
|
|
>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
Joined: Dec 06, 2001
Posts: 3061
|
|
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
|
Java API Documentation
The Java Tutorial
|
 |
faisal usmani
Ranch Hand
Joined: Jan 14, 2006
Posts: 139
|
|
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]
|
 |
 |
|
|
subject: Array assignment
|
|
|