【发布时间】:2014-03-16 04:19:48
【问题描述】:
我正在用 Java 制作地牢游戏。我创建了一个将地图存储在二维数组中的方法。数组如下所示:
[[#, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #],
[#, ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., #],
[#, ., ., ., ., ., ., G, ., ., ., ., ., ., ., ., E, ., #],
[#, ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., #],
[#, ., ., E, ., ., ., ., ., ., ., ., ., ., ., ., ., ., #],
[#, ., ., ., ., ., ., ., ., ., ., G, ., ., ., ., ., ., #],
[#, ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., #],
[#, ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., #],
[#, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #]]
我现在正在尝试编写一个函数,以便可以在游戏中打印出地图。到目前为止,我想出了这个:
public void printMap(char[][] map) {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; i++) {
System.out.print(map[i][j]);
}
}
}
但是,在打印出第一行后,我收到了此错误消息。
#########Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
为什么我会收到此错误消息?以后如何预防?
【问题讨论】:
-
你认为这个错误意味着什么?
-
@JonathonReinhart 不要读博客,写一篇!挫折感。
标签: java arrays printing multidimensional-array indexoutofboundsexception