aspose file tools
The moose likes Beginning Java and the fly likes 2-D array or array of arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "2-D array or array of arrays" Watch "2-D array or array of arrays" New topic
Author

2-D array or array of arrays

J. Kevin Robbins
Ranch Hand

Joined: Dec 16, 2010
Posts: 386
    
    3

I've seen it stated many times that Java does not support 2-D arrays, but does support an array of arrays. This seems like semantics to me and pages like this just confuse the issue.

I'm sure from a CS perspective there is some subtle and highly technical difference between a 2-D array and an array of arrays. Can someone please explain it? A google search just leads to pages like the one above which only muddies the water even more.


"I have a mind like a steel... uh... thingy."
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
In a 2D array the size of the second dimension is fixed i.e. every column will be the same length and every row will be the same length.

In an array of arrays, the length of the enclosed arrays can vary.

So arrays of arrays gives you more flexibility - you can model a 2D array by making all the enclosed arrays the same length, but you also have the option of the enclosed arrays being of different lengths.

Joanne
J. Kevin Robbins
Ranch Hand

Joined: Dec 16, 2010
Posts: 386
    
    3

Thank you! That's exactly what I was looking for.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9955
    
    6

a 2-d array is always going to be a square.

An array of arrays lets you have different size...what is sometimes called a 'ragged array'.

My personal analogy has to do with eggs. You want an array that ultimately holds eggs. a 1-d array may be an egg carton. It has some 'spots' where you can stick an egg (or a spot can be empty). Forget for now that most egg cartons are really 6x2, let's just pretend it is a long line of spots...

a real 2-d array (i.e. a non-java array) would be a tray of 12x12 spots where you can put an egg. you can refer to the egg in position (3,7). You could have a 3-d array of eggs if you stacked those on top of each other.


Java is more like the real world. You put eggs into egg cartons (a 1-D array). Then, you can have a crate that holds egg cartons. The crate can hold cartons of 12, 6, 8, or 18 eggs...but a crate can only hold cartons. You don't put eggs directly in the crate. The crate is like a java array-of-arrays. It is a container of containers.

Then you may have a shipping palate. It can hold many crates. so it is a container of containers of containers

Then you have a truck. It can hold many palates, so it is a container of containers (palates) of containers (crates) of containers (egg cartons)...


Never ascribe to malice that which can be adequately explained by stupidity.
J. Kevin Robbins
Ranch Hand

Joined: Dec 16, 2010
Posts: 386
    
    3

fred rosenberger wrote:a 2-d array is always going to be a square.

An array of arrays lets you have different size...what is sometimes called a 'ragged array'.

My personal analogy has to do with eggs.


Good analogy, thanks. I think this thread will help clear up the confusion for others in the future. I doubt that I'm the only one who has puzzled over the semantics of this.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32712
    
    4
Yes, that link is confusing. It says declare when it means initialise, too.
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 99

fred rosenberger wrote:a 2-d array is always going to be a square.


Don't you mean rectangle?


Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9955
    
    6

Jan Hoppmann wrote:
fred rosenberger wrote:a 2-d array is always going to be a square.


Don't you mean rectangle?

yes.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: 2-D array or array of arrays
 
Similar Threads
Array assignment
array dimension confusion
2 dimensional array problem
why this does not give compile-time error ?
multidimensional array confusion