for循环生成java.lang.ArrayIndexOutOfBoundsException:3

时间:2013-11-27 02:00:52

标签: java arrays

我正在编写一个程序来读取文件并查看该信息是否构成了一个魔术方块,但是我收到了这个错误:在我的sumCol方法中出现了“java.lang.ArrayIndexOutOfBoundsException:3”。特别是下面的行:for(int row = 0; row< square [row] .length; row ++){

public int sumCol(int col) {
    int sum = 0;
    for (int row = 0; row < square[row].length; row++) {
        sum += square[row][col];
    }
    return sum;
}



我不确定是否有必要,但我的班上其他人也是如此:

// ****************************************************************

// Square.java
//
// Define a Square class with methods to create and read in
// info for a square matrix and to compute the sum of a row,
// a col, either diagonal, and whether it is magic.
//         
// ****************************************************************
import java.util.Scanner;

public class Square {

int[][] square;

//--------------------------------------
//create new square of given size
//--------------------------------------
public Square(int size) {

    square = new int[size][size];

    for (int row = 0; row < square.length; row++) {
        for (int col = 0; col < square.length; col++) {
            square[row][col] = row * 10 + col;
        }
    }

}

//--------------------------------------
//return the sum of the values in the given row
//--------------------------------------
public int sumRow(int row) {
    int sum = 0;
    for (int col = 0; col < square.length; col++) {
        sum += square[row][col];
    }
    return sum;
}

//--------------------------------------
//return the sum of the values in the given column
//--------------------------------------
public int sumCol(int col) {
    int sum = 0;
    for (int row = 0; row < square[row].length; row++) {
        sum += square[row][col];
    }
    return sum;
}

//--------------------------------------
//return the sum of the values in the main diagonal
//--------------------------------------
public int sumMainDiag() {
    int sum = 0;
    for (int j = 0; j < square.length; j++) {
        sum += square[j][j];    //you can do this because a square's diagonals have the same coordinate points
    }
    return sum;
}

//--------------------------------------
//return the sum of the values in the other ("reverse") diagonal
//--------------------------------------
public int sumOtherDiag() {
    int sum = 0;
    for (int j = 0; j < square.length; j++) {
        sum += square[j][square.length - 1 - j];
    }
    return sum;
}

//--------------------------------------
//return true if the square is magic (all rows, cols, and diags have
//same sum), false otherwise
//--------------------------------------
public boolean magic() {
    boolean answer = true;
    int sum = sumMainDiag();
    if (sumOtherDiag() != sum) {
        answer = false;
    } else {
        for (int col = 0; col < square.length; col++) {
            if (sum != sumCol(col)) {
                answer = false;
            }
        }
        for (int row = 0; row < square.length; row++) {
            if (sum != sumRow(row)) {
                answer = false;
            }
        }
    }
    return answer;
}

//--------------------------------------
//read info into the square from the input stream associated with the
//Scanner parameter
//--------------------------------------
public void readSquare(Scanner scan) {
    for (int[] square1 : square) {
        for (int col = 0; col < square.length; col++) {
            square1[col] = scan.nextInt();
        }
    }
}

//--------------------------------------
//print the contents of the square, neatly formatted
//--------------------------------------
public void printSquare() {
    for (int[] square1 : square) {
        for (int col = 0; col < square1.length; col++) {
            System.out.print(square1[col] + "\t");
        }
        System.out.println();
    }

}

}

4 个答案:

答案 0 :(得分:1)

变化

row < square[row].length

为:

row < square.length

答案 1 :(得分:1)

也许你是这个意思?

public int sumCol(int col) {
    int sum = 0;
    for (int row = 0; row < square.length; row++) {
        sum += square[row][col];
    }
    return sum;
}

请注意,的数量由square.length给出,而给定行中的数量由square[row].length给出。

答案 2 :(得分:1)

你做错了。使用:

public int sumCol(int col) {
    int sum = 0;
    for (int row = 0; row < square.length; row++) {
        sum += square[row][col];
    }
    return sum;
}

您正在检查行的长度,而不是整个阵列的长度。

答案 3 :(得分:1)

我想你想要

// square.length not square[row].length
for (int row = 0; row < square.length; row++) {
  // Here's where we might check square[row].length to be safe.
  if (col < square[row].length) {
    sum += square[row][col];
  }
}