1. Member
Join Date
Nov 2013
Posts
10
Rep Power
0

## 3D Array -- Adding Elements

I'm working on a project where I need to initialize a multidimensional array where each element in the array is the sum of the indices it took to get there.
So, array[5][7][9] will contain the contain the value 5 + 7 + 9 = 21. However, when I loop through the array to set a value to each index, I come up one index short on each dimension. For example, I only get to array[4][6][8].

Java Code:
```	public static void main(String[] args) throws IllegalAccessException,
InstantiationException {
Integer[][][] array = new Integer[5][7][9];

for(int i = 0; i < array.length; i++){
for(int j = 0; j < array[0].length; j++){
for(int k = 0; k < array[0][0].length; k++){
array[i][j][k] = i + j + k;
}
}
}
System.out.println(array[5][7][9]);
}```
I end up getting an array out of bound exception when I try to print out the last element.

2. ## Re: 3D Array -- Adding Elements

getting an array out of bound exception
The range of valid index values ranges from 0 to the array length-1

3. Member
Join Date
Nov 2013
Posts
10
Rep Power
0

## Re: 3D Array -- Adding Elements

I understand that. I need to understand how I can get the values of each array index to be the sum of the subscripts. So, if I have a 3D array with dimensions 5x7x9, the element array[5][7][9] is 5+7+9 = 21. When I test it though, the maximum index I reach is array[4][6][8].

4. ## Re: 3D Array -- Adding Elements

If an array has n elements, the indexes of the elements are 0, 1, 2, ... ,n-1; so if an array has [5][7][9] elemtents, there doesn't exist an element arrays[5][7][9] ...

kind regards,

Jos

5. ## Re: 3D Array -- Adding Elements

I understand that.
Are you sure that you understand? Your reply said that you did not understand.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•