开发者

Multi-Dimensional Array and ArrayIndexOutOfBoundsException

开发者 https://www.devze.com 2022-12-24 21:55 出处:网络
I have a strange problem which I can\'t fix: A field: private boo开发者_开发问答lean[][][] gaps;

I have a strange problem which I can't fix:

A field:

private boo开发者_开发问答lean[][][] 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?


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号