# Multidimensional Array

• 09-25-2011, 05:32 AM
TyCox94
Multidimensional Array
Need help with this multidimensional array
Code:

`public int [][][] blocks = new int[1,2,3,4,5][1,2,3,4,5][1,2];`
Heres what it says...
Quote:

incompatible types
required: int[][][]
found: int[]
• 09-25-2011, 05:38 AM
Fubarable
Re: Multidimensional Array
That's not how you initialize arrays in Java and you'll want to re-read your tutorial.
• 09-25-2011, 05:39 AM
TyCox94
Re: Multidimensional Array
• 09-25-2011, 05:41 AM
Fubarable
Re: Multidimensional Array
Quote:

Originally Posted by TyCox94

You're not doing what the tutorial is telling you to do. Look again. I'm not even sure what you're trying to do as your code is a bit strange.
• 09-25-2011, 05:50 AM
TyCox94
Re: Multidimensional Array
Ok I see. Here is my array now:

Code:

`public int[][] blocks = {{1,2,3,4,5},{1,2,3,4,5},{1,2}};`
how do i set value in an array? Something like...

Code:

`blocks[1][1][1] = x;`
and
Code:

`blocks[1][1][2] = y;`
• 09-25-2011, 05:53 AM
Fubarable
Re: Multidimensional Array
Best to try it and see what happens.
• 09-25-2011, 09:24 AM
Rameshwar Soni
Re: Multidimensional Array
Quote:

Originally Posted by TyCox94
Ok I see. Here is my array now:

Code:

`public int[][] blocks = {{1,2,3,4,5},{1,2,3,4,5},{1,2}};`

The above was 2 dimensional array being initialized, i think you are asking how to initialize a 3 dimensional array
Code:

```int[][][] arr= {                     {                       { 1,2,3},                       { 4,5,6}                     },                     {                       { 7,8,9},                       { 10,11}                     }                 }```
• 09-25-2011, 10:36 PM
TyCox94
Re: Multidimensional Array
How would i call a specific dimension of the array?
Code:

`System.out.println(arr[1][1][1][1]);`
• 09-25-2011, 10:44 PM
Fubarable
Re: Multidimensional Array
Your array has only two dimensions, and so you will need to use only two square brackets after the variable name. e.g.,

Code:

```System.out.println(arr[0][0]); System.out.println(arr[0][1]); System.out.println(arr[0][3]); System.out.println(arr[1][0]); //....```
Note that often when printing out the complete contents of an array we use a for loop. For a 2-dimensional array, we'll often use two for loops, one nested inside of the other. I believe your tutorials show something like this.
• 09-25-2011, 10:50 PM
TyCox94
Re: Multidimensional Array
Why does

Code:

`System.out.println(arr[0][0]);`
return

Quote:

[I@5c28305d
EDIT: Its actually returning difference values...? This time...
Quote:

[I@6691177
• 09-25-2011, 10:56 PM
Fubarable
Re: Multidimensional Array
Then you must have an array of more than 2 dimensions. You appear to have a 3-dimensional int array. You will need to use a 3rd index then.
• 09-25-2011, 11:00 PM
TyCox94
Re: Multidimensional Array
Oh.... Sorry, thanks for all your help!