Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!

# 2-D array or array of arrays

J. Kevin Robbins
Bartender
Posts: 1801
28
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
Posts: 3742
16
• 2
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
Posts: 1801
28
Thank you! That's exactly what I was looking for.

fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
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
Posts: 1801
28
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
Posts: 49789
69
Yes, that link is confusing. It says declare when it means initialise, too.

Jan Hoppmann
Ranch Hand
Posts: 147
• 1
fred rosenberger wrote:a 2-d array is always going to be a square.

Don't you mean rectangle?

fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Jan Hoppmann wrote:
fred rosenberger wrote:a 2-d array is always going to be a square.

Don't you mean rectangle?

yes.