Java TileMap阵列索引边界外异常

Java TileMap阵列索引边界外异常,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我正在尝试制作一个TileMap,但是当我去渲染TileMap并遍历贴图中的所有整数时,我得到了一个ArrayIndexOutOfBoundsException 以下是渲染方法: public void render(Renderer renderer) { for (int x = 0; x < map[1].length; x++) { for (int y = 0; y < map[0].length; y++) { if (

我正在尝试制作一个TileMap,但是当我去渲染TileMap并遍历贴图中的所有整数时,我得到了一个
ArrayIndexOutOfBoundsException

以下是渲染方法:

public void render(Renderer renderer) {

    for (int x = 0; x < map[1].length; x++) {
        for (int y = 0; y < map[0].length; y++) {

            if (map[y][x] == -1) {
                continue;
            }

            renderer.getGraphics().drawImage(tileSet.get(map[y][x]).getTexture(), (x * tileSize) + xOffs, (y * tileSize) + yOffs, null);
        }   
    }
} 
这就是错误:

Exception in thread "Thread-2" java.lang.ArrayIndexOutOfBoundsException: 3
at game.TileMap.render(TileMap.java:37)
at game.Main.render(Main.java:57)
at engine.GameLoop.render(GameLoop.java:87)
at engine.GameLoop.run(GameLoop.java:66)
at java.lang.Thread.run(Unknown Source)

您对数组的迭代不正确<代码>映射[0]。长度
映射[1]。长度
解析为15,因为:

map[0] == {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
map[1] == {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
因此,当
y
为3(或更大)时,在
map[y][x]
处会出现异常。这是因为您试图访问位于
map[3]
的数组,但它只包含3个元素(数组是基于零的,您似乎知道)


通过调试,我确信您可以找出产生异常的原因,并正确初始化循环。提示是map实际上是一个数组数组,这意味着
map[i]
返回索引
i
处的数组,如上所述。

您的列和行颠倒了

根据您的代码,尤其是此语句
if(map[y][x]=-1)
意味着您希望
y
作为行索引,
x
作为列索引,但是您的循环不正确,
x
y
都对列进行计数。这就是为什么您会得到
ArrayIndexOutOfBoundsException
,因为列多于行,并且您试图访问不存在的行

对于二维阵列(矩阵),当您说

map[i].length
您将获得矩阵第i行的列数

要获取行数,您应该说:

map.length
下面是一个更形象的解释:

//rows     map[row][0],  map[row][1]  map[row][2] 
map[0]  {        A,          0,            0        }
map[1]  {        0,          0,            B        }
map[2]  {        0,          C,            0        }
map[3]  {        D,          0,            0        }
此矩阵的行数为4,因此
map.length==4

此矩阵的列数为3,因此
map[0]。长度==3

现在是值
A
B
C
D
。请记住,数组是零索引的

因此,
A
将位于第0行第0列

map[0][0]; // this gives 'A'
B
将位于第1行第2列

map[1][2]; // this gives 'B'
C
我们可以看第2行第1列吗

map[2][1]; // this gives 'C'
D
将位于第3行第0列

map[3][0]; // this gives 'D'
此外,要使
x
成为行索引,
y
成为列索引,您应该在访问地图时交换
x
y
的位置(
map[y][x]
应该是
map[x][y]

您的循环应该如下所示:

for (int x = 0; x < map.length; x++) {   // use map.length here for rows
    for (int y = 0; y < map[0].length; y++) {  // use map[0].length here for columns
        if (map[x][y] == -1) {   // use x for rows and y for columns
            continue;
        }
        renderer.getGraphics().drawImage(tileSet.get(map[x][y]).getTexture(), (x * tileSize) + xOffs, (y * tileSize) + yOffs, null);
     }
}
for(int x=0;x
注意:


由于列数相等,因此可以使用
map[0].length
作为列数。但是,如果列数不同,则应使用
map[rowIndex].length
获取该特定行的列数。

我相信您在数组中交换了x和y。尝试将所有出现的
map[y][x]
更改为
map[x][y]
for (int x = 0; x < map.length; x++) {   // use map.length here for rows
    for (int y = 0; y < map[0].length; y++) {  // use map[0].length here for columns
        if (map[x][y] == -1) {   // use x for rows and y for columns
            continue;
        }
        renderer.getGraphics().drawImage(tileSet.get(map[x][y]).getTexture(), (x * tileSize) + xOffs, (y * tileSize) + yOffs, null);
     }
}