• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array out of bounds error message  RSS feed

 
C C Campbell
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to print out all of the values of an array and the average. I am getting part of the printout but I am also getting an array out of bound error message. Can anyone tell me what I am doing wrong?

 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carefully look at the loops. The outer for-loop iterates over the points array and the inner for-loop should iterate over the elements of the points array.

But in your code, in the first nested loop the inner loop is iterating over points array and in the second nested loop the outer for-loop is iterating over the elements of the points array. As the size of points array is only 2 but the elements in the point array are of size 3 and hence you see an ArrayOutOfBounds exception
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another couple of things I would change.
You can simplify the declaration of that array:-
int [][] points = new int[][]{{3, 2, 4}, {2, 2, 2}};
If you have the declaration and initialiser on the same line, you are allowed to miss out new int[][], giving you this:-
int [][] points = {{3, 2, 4}, {2, 2, 2}};

If you look in the Java® Tutorials, you will find an example of the for loop. If you are iterating the array in a read‑only fashion, always use the for‑each loop (officially called “enhanced for”). That will guarantee to visit every element of the array and not throw out of bounds exceptions never mind what size array you have. Note carefully the bounds of searching you are using, with − 1. If you have a 10‑element array, your loop will continue going as long as your index is less than 9, so it will only go as far as index 8 and will miss out the last element. There are sometimes good reasons for missing out the last element, for example when you use i + 1 inside the loop, but for normal iteration always use this format of the for loop. Note the spaces make it easier to read:-
for (int i = 0; i < myArray.length; i++) ...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error should tell you on what line you get the OOB error. You can then put a System.out.println just before that line, and print out your array index(es). I will often print out the length of the array(s). Sometimes the array is shorter than I think. Sometimes, my index is more than I think. That's why I want to see what it ACTUALLY is.
 
C C Campbell
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!