• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

What does the dimension of an array refer to?

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags when you post code, so that the code will be formatted automatically which makes it easier to read. Thanks!
 
lowercase baba
Posts: 12911
64
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
It means our mission is in jeapordy! Quick, read this tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic