Getting a Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException error, and am stuck trying to figure out a solution

huangapple go评论58阅读模式
英文:

Getting a Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException error, and am stuck trying to figure out a solution

问题

这是我为学校项目编写的一小段代码。然而,我遇到了一个错误:

线程 "main" 中的异常 "java.lang.ArrayIndexOutOfBoundsException: Index -1 超出了长度 5 的界限
在 TicTacToe.java 的第 25 行 (这是 board[3-w1[i][0]][(w1[i][1])]='O'; 部分)

这是一个示例输入:

5 (数组的大小)

1 0 0 (要放置在棋盘上的黑色棋子数,接着是 B 对坐标表示的黑色棋子的行和列)

1 4 4 (要放置在棋盘上的白色棋子数,接着是 W 对坐标表示的白色棋子的行和列)

4 0 0 1 1 1 1 2 2 2 2 3 1 3 1 4 0 (走棋的次数,接着是 4 个整数:行1 列1 行2 列2,表示一次棋子的移动,即从位置 R1 C1 移动到 R2 C2)

import java.util.Scanner;

public class TicTacToe {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
  
    // 在这里写下你的代码
    int i,j;
    int n=in.nextInt();
    char board[][]=new char[n][n];
    int b=in.nextInt();

    int b1[][]=new int[b][2];
    for(i=0;i<b;i++){
        b1[i][0]=in.nextInt();
        b1[i][1]=in.nextInt();
        board[3-b1[i][0]][b1[i][1]]='*';
    }
    
    int w=in.nextInt();
    int w1[][]=new int[w][2];
    for(i=0;i<w;i++){
        w1[i][0]=in.nextInt();
        w1[i][1]=in.nextInt();
        board[3-w1[i][0]][w1[i][1]]='O';
    }

    for(i=0;i<n;i++){
      for(j=0;j<n;j++){
        if(board[i][j]!='*' && board[i][j]!='O')
          board[i][j]='.';
      }
    }
    int moves=in.nextInt();
    char tag;
    int m[][]=new int[moves*2][2];
    for(i=0;i<(moves*2);i++){
      m[i][0]=in.nextInt();
       m[i][1]=in.nextInt();
    }

    for(i=0;i<(moves*2);i++){
      tag=' ';
      tag=board[3-m[i][0]][m[i][1]];
      board[3-m[i][0]][m[i][1]]='.';
      i++;
      board[3-m[i][0]][m[i][1]]=tag;
    }
    for(i=0;i<n;i++){
      for(j=0;j<n;j++){
        System.out.print(board[i][j]+"");
      }
      System.out.println();
    }
  }
}

是否有方法修复这个错误?

英文:

I'm writing a bit of code for a school project. However, I'm getting a:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 5
at TicTacToe.main(TicTacToe.java:25)

(This is the board[3-w1[i][0]][(w1[i][1])]='O'; bit)

This is a sample input:

5 (size of array)

1 0 0 (the number of black pieces to be placed on the board, followed by B pairs and the row and column of B black pieces)

1 4 4 (the number of white pieces to be placed on the board, followed by W pairs and the row and column of W white pieces)

4 0 0 1 1 1 1 2 2 2 2 3 1 3 1 4 0 (the number of moves, followed by 4 integers: Row 1 Column 1 Row 2 Column 2 which is the row and column of the start and end position of a move. I.e., the piece at R1 C1 is moved to R2 C2)

import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// Write your code here
int i,j;
int n=in.nextInt();
char board[][]=new char[n][n];
int b=in.nextInt();
int b1[][]=new int[b][2];
for(i=0;i&lt;b;i++){
b1[i][0]=in.nextInt();
b1[i][1]=in.nextInt();
board[3-b1[i][0]][b1[i][1]]=&#39;*&#39;;
}
int w=in.nextInt();
int w1[][]=new int[w][2];
for(i=0;i&lt;w;i++){
w1[i][0]=in.nextInt();
w1[i][1]=in.nextInt();
board[3-w1[i][0]][(w1[i][1])]=&#39;O&#39;;
}
for(i=0;i&lt;n;i++){
for(j=0;j&lt;n;j++){
if(board[i][j]!=&#39;*&#39; &amp;&amp; board[i][j]!=&#39;O&#39;)
board[i][j]=&#39;.&#39;;
}
}
int moves=in.nextInt();
char tag;
int m[][]=new int[moves*2][2];
for(i=0;i&lt;(moves*2);i++){
m[i][0]=in.nextInt();
m[i][1]=in.nextInt();
}
for(i=0;i&lt;(moves*2);i++){
tag=&#39; &#39;;
tag=board[3-m[i][0]][m[i][1]];
board[3-m[i][0]][m[i][1]]=&#39;.&#39;;
i++;
board[3-m[i][0]][m[i][1]]=tag;
}
for(i=0;i&lt;n;i++){
for(j=0;j&lt;n;j++){
System.out.print(board[i][j]+&quot;&quot;);
}
System.out.println();
}
}
}

Is there a way to fix the error?

答案1

得分: 1

调试这段代码。

调试基本上意味着:手动浏览代码,并在脑海中弄清楚它应该做什么。如果需要的话,可以使用纸和笔。

然后,运行实际的代码并观察它实际在做什么。通常情况下,人们会使用调试器,但如果那看起来是一项艰巨的任务,那么总是可以选择在代码周围添加大量的System.out.println语句。无论如何,你必须了解系统正在做什么。

然后,在程序运行的过程中,如果与你预期的结果不同,猜猜看,你找到了一个问题!可能会有更多问题,但这绝对是一个问题。调查、修复、重新运行,不断进行,直到应用程序正常工作。

那么,让我们来看这个具体的情况:那个错误意味着你正在访问arr[i],其中arr是一个大小为5的数组,而i为-1,而且Java不知道-1可能是什么意思。鉴于这是board[3-w1[i][0]][(w1[i][1])]='O';这一行,这意味着i为-1,或者w1[i][0]为4(导致3-4得到-1),或者w1[i][1]为-1。这是很多可能性,这正是为什么你需要调试:显然,你认为这段代码不可能出现这些情况中的任何一个为-1,然而,它们却出现了。因此,打印出所有这些东西。如果你无法弄清楚为什么你打印出来的东西是什么,那么找出设置该变量的地方,并在每次调整它时进行打印。最终,你会找出问题所在并解决它。

另一种方法是从神奇的巫术编程公司订购一个水晶球,这就是专业程序员的工作方式:他们只需凝视其中,就能找出错误所在,编写完美的代码,这就是你每天使用的软件为什么百分之百没有bug。唉,多好啊。只要有水晶球就好了,但恐怕并没有这种东西。这就是我们所有人都在做的事情——调试我们的代码。

英文:

Debug this code.

Debugging basically means: Go through the code by 'hand', and figure out in your head what it should be doing. Use pen and paper if you have to.

Then, run the actual code and observe what it actually does. Normally one would use a debugger, but if that feels like a daunting task somehow, hey, there's always the option of litterring a ton of System.out.println statements around. One way or another, you must get insights into what the system is doing.

Then, where the program runs differently than what you thought would happen, guess what? You found one! There may be more, but that's definitely a problem. Investigate, fix, rerun, keep going until the app works.

So, let's get to this specific case: That error means that you are writing arr[i] where arr is an array of size 5, and i is -1, and java has no idea what -1 might mean. Given that it's the board[3-w1[i][0]][(w1[i][1])]=&#39;O&#39;; line, that means i is -1, or, w1[i][0] is 4 (resulting in 3-4, which is -1), or w1[i][1] is -1. That's a lot of options which is exactly why you need to debug: Clearly you think this code is such that none of these things could ever be -1, yet, they are. So, print all that stuff. If you can't figure out why something you print is what it is, then find out where that variable is set and print every time you tweak it. Eventually you'll figure out where the problem lies and fix it.

The alternative is that you order a Crystal Ball from amazing voodoo magic coders Ltd, which is how professional coders work: They just gaze into it and divine where errors are, writing perfect code, which is why software you use daily is 100% bug free. Alas. If only. No such thing as crystal balls, I'm afraid. That's what we all do - debug our code.

huangapple
  • 本文由 发表于 2020年10月6日 06:54:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64217220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定