-2

I am preparing for OCAJP exam, I got a problem with the multi-dimensional arrays in java. After go through a video tutorial on YouTube, I think I got an idea about how it works. It says the following statement creates two double dimensional arrays and one array to hold both arrays. Hence it is a three dimensional array.

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

So I want to get confirmed, that if I want a five dimensional array, this statement would do it.

 int arr[][][] = new int[4][4][3];
user2486322
  • 847
  • 3
  • 13
  • 31

5 Answers5

2

Try to visualise it geometrically.

  • A 1-dimensional array is just a list: new int[2]

  • A 2-dimensional array is a rectangular grid (or a list of lists): new int[2][3]

  • A 3-dimensional array is a cuboid (or a list of rectangles, or a list of lists of lists): new int[2][3][4]

After this it gets harder, but :

  • a 4D array is a list of cuboids (a list of lists of lists of lists) new int[2][3][4][5]

  • a 5D array is a grid of cuboids (a list of lists of lists of lists of lists): new int[2][3][4][5][6]

DNA
  • 42,007
  • 12
  • 107
  • 146
0
int arr[][][] = new int[4][4][3];

Is still a 3 dimensional array.

A 5 dimensional array looks like

 int arr[][][][][] = new int[4][4][3][4][3];
vinayknl
  • 1,242
  • 8
  • 18
0
int arr[][][][][] = new int[4][4][3][X][X];

x can be any number. this is a 5 dimentional array.

user3717646
  • 436
  • 3
  • 10
0

Imagine a cube.

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

Here you have 2 slices of an array of 4x3.

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

With this, you have 4 slices of an array of 4x3.

So, it stills a three dimensional array. However, you can save 4 different two dimensional arrays there.

Hugo Sousa
  • 1,904
  • 2
  • 15
  • 28
0

every time you add a new dimension the number of elements grow exponentially. int[4][4][3] means a 3-dimensions array with 4*4*3=48 elements. to create a 5-dimension array add 2 more square-brackets int[2][2][2][2][2] which is an array with 2^5 elements(2*2*2*2*2)