permaculture playing cards*
The moose likes Beginning Java and the fly likes What does the dimension of an array refer to? 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 » Java » Beginning Java
Bookmark "What does the dimension of an array refer to?" Watch "What does the dimension of an array refer to?" New topic
Author

What does the dimension of an array refer to?

Jake Miller
Ranch Hand

Joined: Jun 27, 2007
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

Joined: Jan 17, 2006
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 ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Pradeep Kadambar
Ranch Hand

Joined: Oct 18, 2004
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

Joined: Aug 16, 2005
Posts: 13884
    
  10

Please use code tags when you post code, so that the code will be formatted automatically which makes it easier to read. Thanks!


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10929
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jake Miller
Ranch Hand

Joined: Jun 27, 2007
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

Joined: Jan 29, 2003
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What does the dimension of an array refer to?
 
Similar Threads
Sorting Issue Help Needed Badly
Arrays doubt
Constructing multidimensional arrays
Multidimensional Arrays
Three dim array