Results 1 to 8 of 8
Thread: Arrays
- 01-12-2013, 01:53 AM #1
Member
- Join Date
- Jan 2013
- Location
- New Zealand
- Posts
- 7
- Rep Power
- 0
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) { } } } } } }
- 01-12-2013, 02:58 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Arrays
What is the exception? If you can't understand it, copy and post the entire stack trace.
- 01-12-2013, 03:11 AM #3
Member
- Join Date
- Jan 2013
- Location
- New Zealand
- Posts
- 7
- Rep Power
- 0
Re: Arrays
Stack trace?
- 01-12-2013, 03:19 AM #4
Member
- Join Date
- Jan 2013
- Location
- New Zealand
- Posts
- 7
- Rep Power
- 0
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) { } } } } } }
- 01-12-2013, 03:33 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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:
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.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>
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"}}, };
- 01-12-2013, 03:36 AM #6
Member
- Join Date
- Jan 2013
- Location
- New Zealand
- Posts
- 7
- Rep Power
- 0
Re: Arrays
And how would I make it so I don't get an error with that array you gave?
- 01-12-2013, 03:49 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Arrays
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.And how would I make it so I don't get an error with that array you gave?
- 01-15-2013, 02:14 AM #8
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 696
- Rep Power
- 1
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
-
Copying Single Arrays to 2-D Arrays
By jmscarlet9 in forum New To JavaReplies: 7Last Post: 04-02-2012, 11:17 PM -
Casting Enum Type arrays to object type arrays
By nmvictor in forum Advanced JavaReplies: 4Last Post: 02-17-2012, 12:49 PM -
arrays and multidimensional arrays
By belfast09 in forum New To JavaReplies: 5Last Post: 06-14-2011, 01:28 PM -
store array of arrays in array of arrays
By joost_m in forum New To JavaReplies: 4Last Post: 04-19-2010, 10:32 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks