Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

What does the dimension of an array refer to?

Jake Miller
Ranch Hand
Posts: 43
I am learning Java, and am only up to as far as arrays go. Here is a code from the book I am using:

class UnevenTwoDimensionArrayInitializer {
public static void main(String args[]) {

//Declare, allocate, initialize
int myarray[][] = {
{33, 71},
{-16, 45, 50, -7},
{99}
};

//Display lengths of the array and its elements
System.out.println("myarray.length = " + myarray.length);
System.out.println("myarray[0].length = " + myarray[0].length);
System.out.println("myarray[1].length = " + myarray[1].length);
System.out.println("myarray[2].length = " + myarray[2].length);

//Display elements
System.out.println(myarray[0][0]);
System.out.println(myarray[0][1]);
System.out.println(myarray[1][0]);
System.out.println(myarray[1][1]);
System.out.println(myarray[1][2]);
System.out.println(myarray[1][3]);
System.out.println(myarray[2][0]);
}
}

Here is the out put:

myarray.length = 3
myarray[0].length = 2
myarray[1].length = 4
myarray[2].length = 1
33
71
-16
45
50
-7
99

This is copied directly from the book so disregard all of the comments and extraneous info.
My question is how is this a 2 dimensional array? I define it as such, but the output of the length of the array is 3, and with the element of the second line 4, I don't see how that could make it 2 dimensions. What does the dimension refer to? I understand it's not a square matrix, but if I were to try to imagine it as a matrix, what would it look like? Sorry re: long post.

Garrett Rowe
Ranch Hand
Posts: 1296
Its an array of int arrays. In Java, thats what all *multi-dimensional* arrays are. They are just arrays of arrays. So its best to the reference to "myarray" as a reference to a single dimensional array of length 3, the element at index 0 (myarray[0]) is a reference to an int[] of length 2, the element at index 1 (myarray[1]) is a reference to an int[] of length 4 and so on. So myArray[x][y] refers to the yth element of the xth array of myArray[][].

see also: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html

Does that help at all?
[ June 27, 2007: Message edited by: Garrett Rowe ]

Pradeep Kadambar
Ranch Hand
Posts: 148
Hi Jake,

Dimensions mean different directions of traversal comparable to x,y axis in 2D and x,y,z in 3D

It is not necessary that the dimensions in these axis are even (like you have in C/C++

So as Garret said ... you can conpare Java arrays to Array of pointers in C

Hope that adds some value

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15568
46
Please use code tags when you post code, so that the code will be formatted automatically which makes it easier to read. Thanks!

fred rosenberger
lowercase baba
Bartender
Posts: 12234
36
In some other languages, you had true multi-dimensional arrays. you would get one big block of contiguous memory, and the indicies could be used to calculate exactly where in the block a specific element would be. I believe that the elements were really stored in that chunk.

Java is different. An array is really nothing more that a list of (basically) addresses telling you where the element REALLY is in the heap. When you define an array, what you are REALLY doing is saying "each element in this array will hold the address of THIS kind of thing". That's why your "System.out.println("myarray.length = " + myarray.length);" prints 3 - your array holds 3 things.

Now, what those three things happen to be are... arrays. You can get the length of each of those 'sub-array' (that's my term, not an official java term). It doesn't really make a lot of sense to ask "what are the 2 dimensions of myarray, since each of the 3 sub-arrays are a different length.

Jake Miller
Ranch Hand
Posts: 43
Awesome, thanks, that does help. I don't know why I was having so much trouble grasping that concept. For some reason I was convincing myself that 2-d meant it was only 2 elements long, completely wrong. Thanks all!

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
In some other languages, you had true multi-dimensional arrays. you would get one big block of contiguous memory, and the indicies could be used to calculate exactly where in the block a specific element would be. I believe that the elements were really stored in that chunk.

Now I'll have nightmares about reading COBOL dumps.