0

I was doing a Tetris tutorial online, and noticed that there is an integer declared like this

int[][][] blah;

Why does the integer have those 3 brackets?

La-comadreja
  • 5,627
  • 11
  • 36
  • 64
MrPork
  • 75
  • 6

6 Answers6

9

It is a three-dimensional array.

Each set of brackets corresponds to an axis. Retrieving a value within the three-dimensional space looks something like this:

int value = blah[x][y][z];

Further Reading
Multi-Dimensional Arrays in Java

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • Despite the 9 current up-votes, I have to ask is this really a three-dimensional array? As far as I can tell from the language specification, Java doesn't appear to have multi-dimensional array support, just support for one-dimensional arrays whose elements can be arrays. As are far as I can tell there is no restriction for member arrays to have a common length, so I say calling this an n-dimensional array is doubly misleading. I would call this an array of arrays of arrays, and add that it could be jagged. By programmatically ensuring it is not jagged, it can mimic a multi-dimensional array. – James World Jan 05 '14 at 14:43
2

It means its a three dimensional array. It can hold the values like:

[
 [1,2]  [4,5]
 [2,3], [6,7], 
]

At above, each values are integer.

[1,2] is an array.

[1,2] 
[2,3]

is a 2d array.

Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
2

It is a 3-dimensional array as what has been said earlier.

You might want to start slow and understand what an array is before going into 3 dimensional. An array is declared as either one of the following ways. It can be used to hold a set of values of the same type (same type as in int, string and so on) instead of having to declare individual variables for each value.

int[] myArray = new int[5];

or

int[] myArray = {1,5,7,1,2};

kar
  • 4,791
  • 12
  • 49
  • 74
1

It is a jagged array of integers - An array of arrays of arrays.

James World
  • 29,019
  • 9
  • 86
  • 120
  • 1
    calling it "jagged" is misleading – La-comadreja Dec 27 '13 at 22:43
  • Why is that? I thought n-dimensional arrays in Java are *not* bound to be rectangular are they? [caveat: my Java knowledge isn't great]. Another answers have compared the declaration to a matrix, which is more misleading since matrices are usually rectangular. – James World Dec 27 '13 at 22:47
  • yes, they are bound to be rectangular and are of fixed length unless reinitialized. – La-comadreja Dec 27 '13 at 22:48
  • 1
    Oh. Quick search showed this: http://xahlee.info/java-a-day/arrays2.html Is this rubbish? – James World Dec 27 '13 at 22:49
  • 1
    According to this, arrays in Java seem to be jagged: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html and the language spec isn't particularly clear, but looks like they are jagged: http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html. – James World Dec 27 '13 at 23:03
  • 1
    This answer also claims Java arrays are jagged: http://stackoverflow.com/questions/5313832/multidimensional-arrays-in-java-and-c-sharp?rq=1. Anyone have a definitive source? – James World Dec 27 '13 at 23:05
  • 1
    Incidentally, the C# language has both jagged (declaration looks like Java) and rectangular (declaration looks like [,,] for a 3D array) arrays. – James World Dec 27 '13 at 23:07
1

It's 3 dimensional array declaration. Such declarations are given because a[5] means something different in different dimensional arrays.So its a declaration to read the references properly.

Vivek Vermani
  • 1,934
  • 18
  • 45
1

It's a three-dimensional matrix of integers.

La-comadreja
  • 5,627
  • 11
  • 36
  • 64