# 4 dimensional array

• November 17th, 2013, 04:48 AM
bodylojohn
4 dimensional array
Hello everybody..

My name is John and I have decided to learn java.

I am fiddling around a bit with it and I want to make declare a 4dimensional int array where EACH array containing 20 cells.

I have this:
Code :

`int[][][][] vierdeDimensie = new int[20][20][20][20];`

Is this correct?

• November 17th, 2013, 04:58 AM
GregBrannon
Re: 4 dimensional array
Arrays contain elements. How many elements total do you need? As you've currently defined the array, it will hold 20 x 20 x 20 x 20 elements, and that's a lot. (But not a big deal for a computer.) What will you use this array for? Some part of a game?
• November 17th, 2013, 05:10 AM
bodylojohn
Re: 4 dimensional array
Quote:

Originally Posted by GregBrannon
Arrays contain elements. How many elements total do you need? As you've currently defined the array, it will hold 20 x 20 x 20 x 20 elements, and that's a lot. (But not a big deal for a computer.) What will you use this array for? Some part of a game?

Thank you very much for replying.

This is a practice exercise I am doing and the exercise literally says:
Declare a 4 dimensional int array where EACH array containing 20 cells.
• November 17th, 2013, 05:21 AM
GregBrannon
Re: 4 dimensional array
Then I'd say you nailed it.
• November 17th, 2013, 05:38 AM
bodylojohn
Re: 4 dimensional array
Thank you!!!!

I tried some other stuff too. Like if I have a one dimensional array of 10 elements and I want to know content of the first position and last position of the string array.
also I think I know how to get the length of the 3rd array.
I came up with:
Code :

```String naam = namen[9]; String naam = namen[0]; int i = arrayvoorbeeld[0][0].length;```

Am I on the right track....?
• November 17th, 2013, 05:59 AM
GregBrannon
Re: 4 dimensional array
Taken together in the same scope, the first two lines would generate an error, but taken separately, the first line assigns the last or 10th element of namen to naam and the second line assigns the first element of namen to naam.

4-dimensional arrays are hard for me to visualize, so when you say you want to find the length of the third array, I'd ask "Which one is the third array?" because I don't know.

In a two-dimensional array, the question could be "How many columns are in the 8th row?" The answer to that would be:

my2DArray[7].length

That I can visualize and understand. 4-D arrays, forget it.
• November 17th, 2013, 06:15 AM
bodylojohn
Re: 4 dimensional array
Quote:

Originally Posted by GregBrannon
Taken together in the same scope, the first two lines would generate an error, but taken separately, the first line assigns the last or 10th element of namen to naam and the second line assigns the first element of namen to naam.

4-dimensional arrays are hard for me to visualize, so when you say you want to find the length of the third array, I'd ask "Which one is the third array?" because I don't know.

In a two-dimensional array, the question could be "How many columns are in the 8th row?" The answer to that would be:

my2DArray[7].length

That I can visualize and understand. 4-D arrays, forget it.

Thank you for replying...

I am doing an excersise to learn java and the exact question (they are all separate question) is:
What command gives the length of the 3rd dimension of an array example.
lets say I have the following 4 dimensional array:
Code :

```int[][][][] arrayvoorbeeld = new int[1][2][3][4]; int i = arrayvoorbeeld[0][0].length;```

The last line of code (int i = arrayvoorbeeld[0][0].length;) gives me the length of the 3rd array.

Am I correct?
• November 17th, 2013, 06:37 AM
GregBrannon
Re: 4 dimensional array
Again, multi-dimensioned arrays greater than 3D are hard to visualize, so let's think this one through. It's important to use specific and precise language. I suspect English is not your native language, and that's fine, you're doing great, but there's a big difference between finding the length of the 3rd dimension and the length of the 3rd array. So let's stick to using "dimension" in this case. Now we have to agree on dimension numbering.

If we were talking about a 2D array, the length of the name of the array, my2DArray.length, specifies the number of rows in my2DArray, so let's call that the 1st dimension. The number of columns in a specific row, or the 2nd dimension, is then found by my2DArray[row].length. Remember that multi-dimensioned arrays don't have to be square in Java. Each row could have a different length or different number of columns.

Following that easier-to-understand pattern, then the length of the dimensions in your 4D array are:
Code :

```arrayvoorbeeld.length; // length of the first dimension or the number of rows arrayvoorbeeld[x].length; // length of the second dimension or the number of columns in row x arrayvoorbeeld[x][y].length; // length of the third dimension or the depth of column (x, y) arrayvoorbeeld[x][y][z].length; // length of the 4th dimension or the time of each depth (x, y, z) // (assuming the 4th dimension is time)```
In your case, everyone of the above for x, y, z = 0 through 19 should be 20, right? You might code that up to verify.
• November 17th, 2013, 11:29 AM
bodylojohn
Re: 4 dimensional array
Quote:

Originally Posted by GregBrannon
Again, multi-dimensioned arrays greater than 3D are hard to visualize, so let's think this one through. It's important to use specific and precise language. I suspect English is not your native language, and that's fine, you're doing great, but there's a big difference between finding the length of the 3rd dimension and the length of the 3rd array. So let's stick to using "dimension" in this case. Now we have to agree on dimension numbering.

If we were talking about a 2D array, the length of the name of the array, my2DArray.length, specifies the number of rows in my2DArray, so let's call that the 1st dimension. The number of columns in a specific row, or the 2nd dimension, is then found by my2DArray[row].length. Remember that multi-dimensioned arrays don't have to be square in Java. Each row could have a different length or different number of columns.

Following that easier-to-understand pattern, then the length of the dimensions in your 4D array are:
Code :

```arrayvoorbeeld.length; // length of the first dimension or the number of rows arrayvoorbeeld[x].length; // length of the second dimension or the number of columns in row x arrayvoorbeeld[x][y].length; // length of the third dimension or the depth of column (x, y) arrayvoorbeeld[x][y][z].length; // length of the 4th dimension or the time of each depth (x, y, z) // (assuming the 4th dimension is time)```
In your case, everyone of the above for x, y, z = 0 through 19 should be 20, right? You might code that up to verify.

Hello,

Thank you very much for your patience and your very detailed explanation!
I meant length of the dimension indeed.

You made it very clear for me.

I am very motivated to continue and learn java.

I will be back with questions, count on that ;-)

Thanks a bunch
• November 17th, 2013, 12:20 PM
GregBrannon
Re: 4 dimensional array
You're welcome. Glad to help.