Results 1 to 8 of 8

Thread: Arrays

  1. #1
    Droid_X is offline Member
    Join Date
    Jan 2013
    Location
    New Zealand
    Posts
    10
    Rep Power
    0

    Default Arrays

    For some reason I get an error on line 12 where I access the array and print all the values.

    Java Code:
    public class blah {
    	public static String[][][] arr = new String[][][]{
    		{{"Hello1","Bye1"},{"Hello2","Bye2"}},
    		{{"Hello3","Bye3"},{"Hello4","Bye4"}},
    		{{"Hello5","Bye5"},{"Hello6","Bye6"}},
    		{{"Hello7","Bye7"},{"Hello8","Bye8"}},
    	};
    	public static void main(String args[]){
    		for (int x = 0; x <= arr.length; x++) {
    			for (int y = 0; y <= arr[x].length; y++){
    				for (int z = 0; z <= arr[y].length; z++){
    					System.out.println(arr[x][y][z]); // On this line.
    					try {
    						 Thread.sleep(50);
    					} 
    					catch(InterruptedException e) {
    					} 
    				}
    			}
    		}
    	}
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Arrays

    What is the exception? If you can't understand it, copy and post the entire stack trace.

  3. #3
    Droid_X is offline Member
    Join Date
    Jan 2013
    Location
    New Zealand
    Posts
    10
    Rep Power
    0

    Default Re: Arrays

    Stack trace?

  4. #4
    Droid_X is offline Member
    Join Date
    Jan 2013
    Location
    New Zealand
    Posts
    10
    Rep Power
    0

    Default Re: Arrays

    I found the answer.

    I was learning Lua previously and the tables/arrays in Lua begin with index 1 instead of 0.

    Java Code:
    public class blah {
    	public static String[][][] arr = new String[][][]{
    		{{"Hello1","Bye1"},{"Hello2","Bye2"}},
    		{{"Hello3","Bye3"},{"Hello4","Bye4"}},
    		{{"Hello5","Bye5"},{"Hello6","Bye6"}},
    		{{"Hello7","Bye7"},{"Hello8","Bye8"}},
    	};
    	public static void main(String args[]){
    		for (int x = 0; x < arr.length; x++) {
    			for (int y = 0; y < arr[x].length; y++){
    				for (int z = 0; z < arr[y].length; z++){
    					System.out.println(arr[x][y][z]);
    					try {
    						 Thread.sleep(50);
    					} 
    					catch(InterruptedException e) {
    					} 
    				}
    			}
    		}
    	}
    }

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Arrays

    I'm glad you've got it sorted out.

    By stack trace I meant the descriptive output that the runtime produces when something goes wrong:

    Java Code:
    C:\Users\Educator\Desktop>javac -cp . Blah.java
    
    C:\Users\Educator\Desktop>java -cp . Blah
    Hello1
    Bye1
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
            at Blah.main(Blah.java:12)
    
    C:\Users\Educator\Desktop>
    Not very long stack trace on this occasion, but "ArrayIndexOutOfBoundsException" is quite useful. The number 2 indicates the value that the runtime was objecting to. In this case you would have to output the values of x, y and z to see which one was out of bounds.

    Notice that you'll get the exception again if you replace your array with:

    Java Code:
    public static String[][][] arr = new String[][][]{
        {{"Hello1","Bye1"},{"Hello2","Bye2"},{"Hello2b","Bye2b"}},
        {{"Hello3","Bye3"},{"Hello4","Bye4"},{"Hello4b","Bye4b"}},
        {{"Hello5","Bye5"},{"Hello6","Bye6"},{"Hello6b","Bye6b"}},
        {{"Hello7","Bye7"},{"Hello8","Bye8"},{"Hello8b","Bye8b"}},
    };

  6. #6
    Droid_X is offline Member
    Join Date
    Jan 2013
    Location
    New Zealand
    Posts
    10
    Rep Power
    0

    Default Re: Arrays

    And how would I make it so I don't get an error with that array you gave?

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Arrays

    And how would I make it so I don't get an error with that array you gave?
    As I suggested, by printing out the values of x, y and z just before the error occurs in order to see which one is out of bounds. Once you've determined that you can go back through your code and check the line that assigned that value.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,620
    Rep Power
    5

    Default Re: Arrays

    You might try it this way instead, since arrays are iterable. That way
    you don't have to worry about array lengths (and its just cleaner).

    Also, unless you have a compelling reason to use arrays, you should probably
    use Lists or some other form of Collection.

    /r,
    Jim

    Java Code:
    public class Blah {
       public static String[][][] arr = new String[][][]{
           {{"Hello1","Bye1"},{"Hello2","Bye2"}},
           {{"Hello3","Bye3"},{"Hello4","Bye4"}},
           {{"Hello5","Bye5"},{"Hello6","Bye6"}},
           {{"Hello7","Bye7"},{"Hello8","Bye8"}},
       };
       public static void main(String args[]){
           for (String [][] a : arr) {
              for (String [] b : a) {
                 for (String c : b) { 
                    System.out.println(c);
                 }
              }
           }
       }
    }

Similar Threads

  1. Copying Single Arrays to 2-D Arrays
    By jmscarlet9 in forum New To Java
    Replies: 7
    Last Post: 04-02-2012, 11:17 PM
  2. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 12:49 PM
  3. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 01:28 PM
  4. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  5. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM

Posting Permissions

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