Results 1 to 5 of 5
  1. #1
    mynorka is offline Member
    Join Date
    Nov 2013
    Posts
    10
    Rep Power
    0

    Default 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. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,441
    Rep Power
    25

    Default 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
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    mynorka is offline Member
    Join Date
    Nov 2013
    Posts
    10
    Rep Power
    0

    Default 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. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,441
    Rep Power
    25

    Default Re: 3D Array -- Adding Elements

    I understand that.
    Are you sure that you understand? Your reply said that you did not understand.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: 04-05-2011, 08:58 PM
  2. Adding elements to an array
    By slitka in forum New To Java
    Replies: 9
    Last Post: 04-01-2011, 04:01 AM
  3. Adding elements before and after array
    By liQuorice in forum New To Java
    Replies: 4
    Last Post: 03-04-2011, 12:52 AM
  4. Adding the Elements of a 2D Array
    By RMcLuckie45 in forum New To Java
    Replies: 0
    Last Post: 11-07-2010, 11:04 PM
  5. Adding elements to an Object Array
    By aneesahamedaa in forum New To Java
    Replies: 4
    Last Post: 09-07-2008, 03:55 PM

Posting Permissions

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