# Need Help! Out of bounds error on ARRAYS!!

• March 17th, 2012, 09:53 PM
kram
Need Help! Out of bounds error on ARRAYS!!
---Example: 2 2 3 20 10 7 9 6 8 8 8 2 9 12 14 5 5 5 5 19
plateau = 6 8 8 8 2 because 8 is the longest sequence of identical
numbers where the numbers to the left and right are lower.

MY ERROR: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 500
at Lab8.main(Lab8.java:60)

Code Java:

```int [] arr3 = new int [500]; for(int i = 0; i < arr3.length;i++){ int y = gen.nextInt(20)+1; arr3[i] = y; System.out.print(" " + y); } int tempBookend1 = 0; int tempBookend2 = 0; int tempPos1 = 0; int tempPos2 = 0; int tempLength = 0; int tempType = 0; int bookend1 = 0; int bookend2 = 0; int pos1 = 0; int pos2 = 0; int length = 0; int type = 0; for(int i = 0; i < arr3.length;i++){ for(int j = 1; j <= arr3.length-2;j++){ if( tempBookend1 < arr3[tempPos1] && tempBookend2 < arr3[tempPos2] && tempLength > length){ bookend1 = tempBookend1; bookend2 = tempBookend2; pos1 = tempPos1; pos2 = tempPos2; length = tempLength; type = tempType; } else if(arr3[i] == arr3[i+1]){ // I get an error here tempPos1 = i; tempBookend1 = arr3[i-1]; } else if(arr3[i] != arr3[i+1]){ // I get an error here tempPos2 = i; tempBookend2 = arr3[i+1]; tempType = arr3[i]; tempLength = (tempPos2 - tempPos1); } } }```
• March 17th, 2012, 10:02 PM
pbrockway2
Re: Need Help! Out of bounds error on ARRAYS!!
The outer for loop indicates that i can grow as large as arr3.length-1. But, on the two lines you indicate as troublesome you use the expression arr3[i+1] which is past the end of the array and, hence, liable to cause an ArrayIndexOutOfBoundsException.

You should rethink the logic of the loops so that you don't attempt to access the array past its end (ie past index arr3.length-1).

Doing this will involve reading the question closely to determine whether a run of large numbers right at the end of the array counts as a plateau or not.