0

This has always bugged me so I'm bringing it to the community in the hopes that someone can help me. Let's say I have a 2D array such as

int[][] arr = new int[2][3];

I have never really been able to figure out how to think about this array. Is it a 2 x 3 or a 3 x 2 structure. I could write methods to print out both combinations.

Or another way to think about this is that a 2D array is simply an array of arrays. But is it an array of length 3 where each component is an array of length 2, or is it an array of length 2, where each component is an array of length 3.

Effectively, which dimension does the 2 refer to and which does the 3 refer to, or does it even matter?

zephyr
  • 2,182
  • 3
  • 29
  • 51

4 Answers4

2

It is an array of 2 arrays, and each of them is an array of 3 elements. Simple as that.

Ammaro
  • 370
  • 2
  • 14
1

You could think of a 2D array as either a "x by y" structure or a "y by x" structure, by changing which variable access which dimension. So, it could be either a 2 x 3 or a 3 x 2 structure.

But there is an order to the dimensions. A int[2][3] is an array of 2 int[3]s, each of which holds 3 ints.

There is no inherent meaning in the dimensions other than the order in which they are specified. You can assign any meaning to each dimension.

rgettman
  • 176,041
  • 30
  • 275
  • 357
0

A 3-step process:

// create the single reference nums (yellow square)

 int [][] nums;

// create the array of references (blue squares)

nums = new int[5][];

// this create the second level of arrays (red squares)

for (int i=0; i < 5 ; i++)
   nums[i] = new int[4]; // create arrays of integers

Note: when you initially declare a 2D array:

you must always specify the first dimension nums = new int[][]; // ILLEGAL - NEEDS 1ST DIMENSION

you do not need to specify the second dimension

nums = new int[5][]; // OK
nums = new int[5][4]; // OK

Elements of the Array: if nums is a 2D array as shown above,

nums[i][j] // represents a single integer in that array
nums[i] //represents a 1D array (a single row in the 2D array)

Please See the link for more info on the Subject.

http://www.processing.org/tutorials/2darray/

http://www.leepoint.net/notes-java/data/arrays/arrays-2D.html

http://www.willamette.edu/~gorr/classes/cs231/lectures/chapter9/arrays2d.htm

Naruto
  • 20
  • 2
0

Two-dimensional arrays are defined as "an array of arrays". Since an array type is a first-class Java type, we can have an array of ints, an array of Strings, or an array of Objects. For example, an array of ints will have the type int[]. Similarly we can have int[][], which represents an "array of arrays of ints". Such an array is said to be a two-dimensional array. The command

 int[][] A = new int[2][3];

declares a variable, A, of type int[][], and it initializes that variable to refer to a newly created object. That object is an array of arrays of ints. Here, the notation int[2][3] indicates that there are 2 arrays of ints in the array A, and that there are 3 ints in each of those arrays.