-2

最后一个 for 循环将编译但不会运行。它说数组索引超出范围异常:17。我只想在第 17 行的 1-8 列中添加一个 ColorRectangles(ColorShape 的子类)

private ColorShape[][] _tiles; 

public GamePanel()
{
   _tiles = new ColorShape[8][17];

  for (int i = 0; i<16; i++){
          for(int j=0; j<8;j++){
              _tiles[j][i] = null;
          }
     }

  for (int j=0; j<8;j++){
            _tiles[j][17] = new ColorRectangle(Color.BLACK);

     }
}
4

8 回答 8

0

这个:

_tiles[j][17] = new ColorRectangle(Color.BLACK);

17 不是您的数组的有效索引,因为您将其大小设置为[8][17]. 索引来自0...length - 1,因此在您的情况下,最多为 16。这是因为 Java(以及任何合理的编程语言)中的数组是零索引的。因此,替换为

_tiles[j][16] = new ColorRectangle(Color.BLACK);

至少你不会得到一个ArrayIndexOutOfBoundsException.

我只想在第 17 行的 1-8 列中添加一个 ColorRectangles(ColorShape 的子类)

请看,在从零开始的索引方案中,第 17 行索引为 16 的行。

第一行对应索引为 0 的行。第二行对应索引为 1 的行。依此类推。一般来说,nth行对应索引n - 1的行,索引的行n实际上是(n + 1)th行(假设0 <= n < length - 1)。

于 2013-07-05T15:04:17.633 回答
0

你的问题在这里:

_tiles[j][17] = new ColorRectangle(Color.BLACK);

你应该有这个(注意数组是零索引的,所以第 17 列在索引 16 处):

_tiles[j][16] = new ColorRectangle(Color.BLACK);  
于 2013-07-05T15:04:17.927 回答
0

数组的长度是,[8][17]但索引从[0][16]。因此更换

_tiles[j][17] = new ColorRectangle(Color.BLACK);

_tiles[j][16] = new ColorRectangle(Color.BLACK);
于 2013-07-05T15:05:25.257 回答
0

您在代码中使用 x 范围内的 8 个字段和 y 范围内的 17 个字段声明数组:

 _tiles = new ColorShape[8][17];

因为您在 IT 中从零开始计数,所以范围是从零到十六 ( 0-16)。
因此,如果您想要数组的最后一个字段,则必须使用字段 16 ( _tiles[j][16])。

代码应该是:

_tiles[j][16] = new ColorRectangle(Color.BLACK);

整个事情被称为从零开始的编号/索引

于 2013-07-05T15:05:28.453 回答
0

您的数组大小为 17。数组是零索引的。这意味着您的第二维的最大数组索引实际上是 16,而不是 17。

将您对瓷砖的引用更改为:

_tiles[j][16] = new ColorRectangle(Color.BLACK);

或使您的 _tiles 数组更大,如下所示:

_tiles = new ColorShape[8][18];

要么解决这个问题。

我还可以建议您的游戏面板对象接受 2 维,宽度和高度。如果您决定再次使用它或更改其尺寸,这将使您的类更有用。适当时避免硬编码值。这里应该是最合适的。

这是给你的重写:

private ColorShape[][] _tiles; 

public GamePanel(int width, int height)
{
   _tiles = new ColorShape[width][height];

   for (int i = 0; i<height; i++){
      for(int j=0; j<width;j++){
          _tiles[j][i] = null;
      }
   }

  for (int j=0; j<width;j++){
        _tiles[j][tiles[j].length-1] = new ColorRectangle(Color.BLACK);

 }
}

以下是在您的情况下使用它的方法:

GamePanel panel = new GamePanel(8, 17);

但是您现在可以随时轻松更改面板尺寸!

GamePanel panel = new GamePanel(100,100);

漂亮吧?

于 2013-07-05T15:04:30.087 回答
0

在java中,索引从零(0)开始;

 private ColorShape[][] _tiles; 

 public GamePanel()
 {
    _tiles = new ColorShape[8][18]; // new dim is 18 so last index is 17

   for (int i = 0; i<18; i++){
           for(int j=0; j<8;j++){
               _tiles[j][i] = null;
           }
      }

   for (int j=0; j<8;j++){
             _tiles[j][17] = new ColorRectangle(Color.BLACK); //17 was out of bounds

      }
 }
于 2013-07-05T15:04:31.513 回答
0

根据JLS(阵列访问) -All arrays are 0-origin. An array with length n can be indexed by the integers 0 to n-1.

所以如果你像你一样初始化一个数组_tiles = new ColorShape[8][17];。为了访问,您需要通过0 to 7and进行操作0 to 16

现在,_tiles[j][17] = new ColorRectangle(Color.BLACK);您正在尝试访问您已初始化的数组之外的东西,因此您得到java.lang.ArrayIndexOutOfBoundsException.

于 2013-07-05T15:07:27.357 回答
0

您可能还有第一个循环的错误。
而不是
for (int i = 0; i<16; i++)
应该
for (int i = 0; i<17; i++)

数组是零索引的,这意味着起始索引是 0 到长度为 1。

因此,在您的情况下,最多为 16 个。

对于你的第二个循环,它应该是

_tiles[j][16] = new ColorRectangle(Color.BLACK);

于 2013-07-05T15:07:46.337 回答