开发者

Array Out of Index Error?

I'm using some new methods that I don't usually use(random.nextInt()), so help is greatly appreciated.

  int[][] A = new int[100][100];
  private void room()
  {
    int xCo = random.nextInt(92);
    int yCo = random.nextInt(92);
    int xCoPLUS = random.nextInt(7);
    int yCoPLUS = random.nextInt(7);
    for(int across = xCo开发者_开发知识库; across < across+xCoPLUS; across++) 
    {
      for(int vert = yCo; vert < vert+yCoPLUS; vert++)
      {
        A[vert][across] = 1;
      }
    }
  }

I'm getting the error at the line A[vert][across] = 1;

A[][] is 100 by 100.

Thanks guys!


I think you really mean:

for(int across = xCo; across < xCo+xCoPLUS; across++) 
{
   for(int vert = yCo; vert < yCo+yCoPLUS; vert++)
   {
      A[vert][across] = 1;
   }
}

Note the change I made to your loop conditions, as across < across + xCoPLUS will always be true, at least until you get an integer overflow :)


The loop conditions across < across+xCoPLUS and vert < vert+yCoPLUS will always stay true given xCoPLUS and yCoPlus are non-zero, thus your loops will loop indefinitely, thus walking past the end of the array.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜