Reputation: 13
I have a strange problem which I can't fix:
A field:
private boolean[][][] gaps;
Constructor (1st line):
gaps = new boolean[NOBARRICADES][WIDTH][HEIGHT];
Constructor (2nd line):
for (int i = 0; i < NOBARRICADES; i++) {
Java throws an error for the 2nd line, saying:
Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException
Does it have anything to do with Java syntax (the mistake is in these lines of code) or I should look for the problem somewhere else?
Upvotes: 1
Views: 312
Reputation: 62769
Sorry, but you really don't want to do that.
Multidimensional arrays are never worth the confusion they cause--they have no positive value at all (with the POSSIBLE exception of a clear, obvious x,y array).
I suggest you try starting with either a list of two-dimensional arrays or a two-dimensional array of objects where each object contains a list.
Upvotes: 0
Reputation: 17422
You're probably misreading the error output. Your second line does not even access the array - make sure that it's not the first line of the body of the for-loop that throws the exception. Also, make sure that you use i
only to index the first dimension of your array.
Upvotes: 1
Reputation: 16655
sometimes the java compiler is off by a line or two. You may check the lines of code around the line that it says the error is on and see if you see anything.
Upvotes: 0