简体   繁体   English

为什么会出现“ ArrayIndexOutOfBoundsException”错误?

[英]Why am I getting an “ArrayIndexOutOfBoundsException” error?

I have written a program in which the user inputs the dimensions of a two-dimensional array, then the computer prints out the table with those dimensions and fills it with random integers from 0 to 9. Then, if there are four consecutive equal integers that appear anywhere in the table, the computer would return "True". 我编写了一个程序,其中用户输入二维数组的维,然后计算机打印出具有这些维的表,并用0到9的随机整数填充表。然后,如果有四个连续的相等整数出现在表格中的任何位置时,计算机将返回“ True”。 If there are no consecutive equal integers in the table, it would return "False". 如果表中没有连续的相等整数,它将返回“ False”。 For instance: 例如:

2 5 8 7 1
3 2 9 4 7
5 1 2 0 3
8 0 1 2 7

In that table, two appear consecutively, diagonally from the first spot. 在该表中,两个从第一个点对角线连续出现。 It can also be like this: 也可以像这样:

9 5 3 7 0
2 5 7 3 1
8 5 0 2 9
4 5 1 7 5

In this table, five appear vertically down from the second spot. 在此表中,从第二个点垂直向下出现五个。

Here is the code I have written: 这是我编写的代码:

/*MyName*/
package fourconsecutivenumbers;
import java.util.Random;
import java.util.Scanner;
public class FourConsecutiveNumbers {

public static void main(String[] args) {

    Scanner rowDimension = new Scanner(System.in);
    System.out.print("Enter the number of rows: ");
    int firstInput = rowDimension.nextInt();

    Scanner columnDimension = new Scanner(System.in);
    System.out.print("Enter the number of columns: ");
    int secondInput = columnDimension.nextInt();

    int[][] randomTable = new int[firstInput][secondInput];
    for (int row = 0; row < firstInput; row++) {
        for (int column = 0; column < secondInput; column++) {
            randomTable[row][column] = (int)(Math.random() * 10 + 0);
            System.out.print(randomTable[row][column] + " ");
        }
        System.out.println();
    }
    if(check_horizontal(randomTable) || check_vertical(randomTable) || check_diagonal(randomTable)){
   System.out.println("True");
   }
   else {
    System.out.println("False");
    }
   }


public static boolean check_vertical(int[][] randomTable) {

    for (int i = 0; i<randomTable.length-3; i++){
        for(int t=0; t<randomTable[0].length-3;t++){
        if (randomTable[i][t]==randomTable[i][t+1] && randomTable[i][t+1]==randomTable[i][t+2] && randomTable[i][t+2]==randomTable[i][t+3]){
            return true;
        }
    }
}
return false;
}
 public static boolean check_horizontal(int[][] randomTable){

     for (int i = 0; i<randomTable.length; i++){
        for(int t=0; t<randomTable[0].length;t++){
        if (randomTable[t][i]==randomTable[t+1][i] && randomTable[t+1][i]==randomTable[t+2][i] && randomTable[t+2][i]==randomTable[t+3][i]){
            return true;
        }
    }

}
return false;
}
 public static boolean check_diagonal(int[][] randomTable){
//check down    
for (int t =0; t<randomTable.length-3; t++){
    for(int i=0; i<randomTable[0].length-3;i++){
        if (randomTable[i][t]==randomTable[i+1][t+1] && randomTable[i+1][t+1]==randomTable[i+2][t+2] && randomTable[i+2][t+2]==randomTable[i+3][t+3]){
            return true;
        }

    }
}
//check up
for (int t = 3; t < randomTable.length; t++){
for(int i=0; i<randomTable[0].length-3;i++){
    if (randomTable[t][i]==randomTable[t-1][i+1] && randomTable[t-1][i+1]==randomTable[t-2][i+2] && randomTable[t-2][i+2]==randomTable[t-3][i+3]){
        return true;
    }

}
}
return false;
}
}

Now the error comes up whenever I put in different dimensions. 现在,只要我放入不同的尺寸,就会出现错误。 Like a 5x10 table or a 10x5 table...but if I were to do a 5x5 table or a 10x10 table, it executes. 就像5x10表格或10x5表格...但是如果我要制作5x5表格或10x10表格,它将执行。 The error I get is... 我得到的错误是...

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at fourconsecutivenumbers.FourConsecutiveNumbers.check_horizontal(FourConsecutiveNumbers.java:49)
at fourconsecutivenumbers.FourConsecutiveNumbers.main(FourConsecutiveNumbers.java:25)
Java Result: 1

I am new to programming but I know the error is from line 49. Maybe I wrote the for loop wrong? 我是编程新手,但我知道错误来自第49行。也许我写了for循环错误? I don't know what I did wrong really and I was hoping someone can show me what I did wrong and how to fix it so I can learn what not to do. 我不知道我确实做错了什么,我希望有人可以告诉我我做错了什么以及如何解决,以便我可以了解不该做的事情。

In the check_horizontal method you have your i's and t's back to front 在check_horizo​​ntal方法中,您的i和t背对着前面

if (randomTable[t][i]==randomTable[t+1][i] &&
    randomTable[t+1][i]==randomTable[t+2][i] && 
    randomTable[t+2][i]==randomTable[t+3][i]){
  return true;
}

Should be 应该

if (randomTable[i][t]==randomTable[i+1][t] &&
    randomTable[i+1][t]==randomTable[i+2][t] && 
    randomTable[i+2][t]==randomTable[i+3][t]){
  return true;
}

And as pointed out in the comments your loop conditions are also incorrect: 并且正如注释中指出的那样,循环条件也不正确:

for (int i = 0; i<randomTable.length; i++){
   for(int t=0; t<randomTable[0].length;t++){

Should be 应该

for (int i = 0; i<randomTable.length - 3; i++){
   for(int t=0; t<randomTable[i].length;t++){

This is checking vertically not horizontally as the name suggests, but as check_vertical is actually checking horizontal this will not adversely effect your program (just your marks). 顾名思义,这不是在垂直方向上进行水平检查,而是由于check_vertical实际上是在水平方向进行检查,因此不会对您的程序产生不利影响(仅会影响您的标记)。

The first point (i's and t's mixed up) is due to which lengths you are checking i is limited to the length of randomTable (that is the number of rows) while t is limited to the length of the zeroth array t < randomTable[0].length (that is the number of columns) yet in your code you are using i as an index into columns and t as an index into row. 第一点(i和t混合在一起)是由于要检查的长度,i限制为randomTable的长度(即行数),而t限制为第零个数组的长度t < randomTable[0].length (即列数),但是在您的代码中,您使用i作为列的索引,使用t作为行的索引。

This may be easier to understand if you renamed i into row, and t into column something like: 如果将i重命名为row,将t重命名为column,则可能更容易理解:

private int getNumberOfRows(int[][] table) { return table.length; }
private int getNumberOfColumnsInRow(int[][] table, int row) { return table[row].length }
...
for (int row = 0; row < getNumberOfRows(randomTable) - 3; row++){
   for(int column = 0; column<getNumberOfColumnsInRow(randomTable,i);column++){

For the second point loop conditions incorrect as you are adding up to 3 to the row index (the first number in square brackets) you need to ensure there are enough at least 3 more rows after row . 对于第二个点循环条件不正确,因为您要将行索引加3(第一个数字放在方括号中),则需要确保row之后至少还有3 row

In the check_horizontal you have swapped i and t. 在check_horizo​​ntal中,您交换了i和t。 From your loops 从你的循环

for (int i = 0; i<randomTable.length; i++){
    for(int t=0; t<randomTable[0].length;t++){

you would need to access the array with 您将需要使用

randomTable[i][t]

and not with randomTable[t][i] 而不是与randomTable [t] [i]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM