I am getting an array index out of bounds exception in the following code:
for (int i=1; i<11; i++) {
int a[][] = new int[10][3];
double LeftTrim = 1.0;
double RightTrim = 1.0;
a [i][0]=(int) (LeftTrim*((i)*25));
a [i][1]=(int) (RightTrim*((i)*25));
a [i][2]= 5000;
//leftWheel, rightWheel, Milliseconds
myf.setWheelVelocities(a[i][0], a[i][1], a[i][2]);
JOptionPane.showMessageDialog(null, + (a [i][0] + a [i][1])/2 + "wheel velocities" + " | " + a [i][2] + " Milliseconds" + " Click OK to continue...");
}
Every-time I reach the 9th increment Eclipse gives me the error
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10"
I have to test the velocity up to 250, but when I reach 225 and I click ‘Ok’ on ‘Click ok to continue…” this error shows up! Am I going out of the array bounds or something?
EDIT: The speed velocity has to start from 25, 50, 75 … 250 (so ten in all)
Thank you!
The valid values of
iina[i]are0to9. Your code iterates to10.Also, the loop starts from
1whereas it might need to start from0.Finally, the fact that you’re allocating the 2D array inside the loop look pretty odd. Either move it outside the loop, or turn it into a three-element 1D array.