File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

2-D array or array of arrays

 
J. Kevin Robbins
Bartender
Pie
Posts: 1605
15
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
J. Kevin Robbins
Bartender
Pie
Posts: 1605
15
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! That's exactly what I was looking for.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)...
 
J. Kevin Robbins
Bartender
Pie
Posts: 1605
15
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that link is confusing. It says declare when it means initialise, too.
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:a 2-d array is always going to be a square.


Don't you mean rectangle?
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Hoppmann wrote:
fred rosenberger wrote:a 2-d array is always going to be a square.


Don't you mean rectangle?

yes.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic