【问题标题】:How to fix "Index 5 out of bounds for length 5" error in Java如何修复 Java 中的“索引 5 超出长度 5 的范围”错误
【发布时间】:2019-11-01 04:22:30
【问题描述】:

我正在研究 CCC 2018 Robo Thieves 问题的解决方案,它是原始问题的简化版本。我的问题是,当我执行我的代码时,它会给我这个“索引 5 超出长度 5 的范围”,我不确定它为什么会发生。我的程序执行了一半,然后出现这个错误。

import java.util.*;

public class RoboThieves {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Integer> rowPos = new ArrayList<Integer>();
        ArrayList<Integer> colPos = new ArrayList<Integer>();
        int sRow = -1; // robot pos
        int sCol = -1;
        int dotCounter = 0;
        int stepCounter = 0;

        int rowSize = sc.nextInt();
        int colSize = sc.nextInt();
        char[][] factoryGrid = new char[rowSize][colSize];

        for (int i = 0; i < rowSize; i++) {

            String rowChars = sc.next().toUpperCase();

            for (int j = 0; j < colSize; j++) {

                factoryGrid[i][j] = rowChars.charAt(j);
            }

        }

        // check to see if the grid was inputted properly (with square brackets)
        /*
         * for (char [] row: factoryGrid) { System.out.println(Arrays.toString(row)); }
         */

        // check to see if the grid was inputted properly (as inputted)
        for (int i = 0; i < rowSize; i++) {
            for (int j = 0; j < colSize; j++) {

                System.out.print(factoryGrid[i][j]);
            }
            System.out.println();
        }

        // locate dots and store their row and col in arraylists
        for (int i = 0; i < rowSize; i++) {

            for (int j = 0; j < colSize; j++) {
                if (factoryGrid[i][j] == '.') {
                    rowPos.add(i);
                    colPos.add(j);
                    dotCounter++;
                }
            }

        }

        // print dot location to check
        for (int i = 0; i < rowPos.size(); i++) {
            System.out.println("Dot Position = " + "(" + rowPos.get(i) + "," + colPos.get(i) + ")");
        }

        // locate robot position
        for (int i = 0; i < rowSize; i++) {

            for (int j = 0; j < colSize; j++) {
                if (factoryGrid[i][j] == 'S')
                    sRow = i;
                    sCol = j;
            }

        }

        // print camera location to check
        System.out.println("Camera Position = " + "(" + sRow + "," + sCol + ")");

        //System.out.println(dotCounter); // test to see if counter works

        char above = getAbove(factoryGrid, sRow, sCol);
        char right = getRight(factoryGrid, sRow, sCol);
        char below = getBelow(factoryGrid, sRow, sCol);
        char left = getLeft(factoryGrid, sRow, sCol);

        if (above == '.') {

            boolean canMove = check360(factoryGrid, sRow, sCol);
            // check if camera is around dot
            if (canMove == true) {
                // set robot position to dot position and old position to W
                sRow = sRow - 1;
                // sCol = sCol;

                factoryGrid[sRow][sCol] = 'W';

                dotCounter--;
                stepCounter++;

            } else {
                // this is if there is a camera in the 360 radius of the open space
                System.out.println("You cannot move to the space beside because there is a camera in your sightline!");
            }

        } else if (right == '.') {

            boolean canMove = check360(factoryGrid, sRow, sCol);
            // check if camera is around dot
            if (canMove == true) {
                // set robot position to dot position and old position to W
                // sRow = sRow;
                sCol = sCol + 1;

                factoryGrid[sRow][sCol] = 'W';

                dotCounter--;
                stepCounter++;

            } else {
                // this is if there is a camera in the 360 radius of the open space
                System.out.println("You cannot move to the space beside because there is a camera in your sightline!");
            }

        } else if (below == '.') {

            boolean canMove = check360(factoryGrid, sRow, sCol);
            // check if camera is around dot
            if (canMove == true) {
                // set robot position to dot position and old position to W
                sRow = sRow + 1;
                // sCol = sCol;

                factoryGrid[sRow][sCol] = 'W';

                dotCounter--;
                stepCounter++;

            } else {
                // this is if there is a camera in the 360 radius of the open space
                System.out.println("You cannot move to the space beside because there is a camera in your sightline!");
            }

        } else if (left == '.') {
            boolean canMove = check360(factoryGrid, sRow, sCol);
            // check if camera is around dot
            if (canMove == true) {
                // set robot position to dot position and old position to W
                // sRow = sRow;
                sCol = sCol - 1;

                factoryGrid[sRow][sCol] = 'W';

                dotCounter--;
                stepCounter++;

            } else {
                // this is if there is a camera in the 360 radius of the open space
                System.out.println("You cannot move to the space beside because there is a camera in your sightline!");
            }
        } else {

            System.out.println(
                    "The robot cannot move to any spaces try inputting a factory layout that can produce an answer.");

        } // end if above dot (yes)

        System.out.println(stepCounter);

        for (int i = 0; i < rowSize; i++) {
            for (int j = 0; j < colSize; j++) {

                System.out.print(factoryGrid[i][j]);
            }
            System.out.println();
        }

    } // end main method

    public static char getLeft(char[][] factGrid, int cRow, int cCol) {
        return factGrid[cRow][(cCol - 1)];

    }

    public static char getAbove(char[][] factGrid, int cRow, int cCol) {
        return factGrid[(cRow - 1)][(cCol)];

    }

    public static char getBelow(char[][] factGrid, int cRow, int cCol) {
        return factGrid[cRow + 1][cCol];

    }

    public static char getRight(char[][] factGrid, int cRow, int cCol) {
        return factGrid[cRow][(cCol + 1)];

    }

    public static boolean check360(char[][] factGrid, int cRow, int cCol) {
        boolean canMove = true;

        char left = getLeft(factGrid, cRow, cCol);
        char above = getAbove(factGrid, cRow, cCol);
        char right = getRight(factGrid, cRow, cCol);
        char below = getBelow(factGrid, cRow, cCol);

        if (left == 'C' || above == 'C' || right == 'C' || below == 'C') {
            canMove = false;
        }
        return canMove;
    }

} // end main program

【问题讨论】:

  • 在不尝试调试代码的情况下,它还应该告诉您哪一行出错了。这是一个很大的线索。索引 5 超出了长度为 5 的列表的范围(索引从 0 到 4)。查看报告的行并找出您为什么走得太远 - 或者 - 为什么您的列表中没有足够的项目。

标签: java


【解决方案1】:

乍一看,我认为问题可能在于您的 getLeft()、getAbove()、getRight() 和 getBelow() 方法。

在这些方法中,你给它一个 cRow 和 cCol 的值并加或减 1。但是,你需要确保你正在查询的索引不超过双精度数组的大小 factGrid em>,或低于 0。

例如,在您的 getRight() 方法中,您可以尝试:

public static char getRight(char[][] factGrid, int cRow, int cCol) {
    if(factGrid[0].length > (cCol + 1)) {
        return factGrid[cRow][(cCol + 1)];
    } else {
        return '';
    }
}

【讨论】:

  • 这是正确的,或者就像(cCol + 1) &gt; factGrid[0].length - 这可能会以异常结束,最好使用&lt;(对不起,我更喜欢这个order,因为它读取更自然的海事组织)
  • 顺序并不重要,只要有合适的标志即可。我确实意识到我在 (
  • 我知道这对编译器/机器无关紧要 - 但对人的顺序可能很重要(在我看来它更容易阅读,更自然 - 我愿意不想知道“长度是否大于某事”,但我想知道“索引是否大于某事” - 见Is Code For Computers or for People - 仍然是个人喜好问题,再次,只是我的观点,当然你可能更喜欢这种方式 - 也有效)
猜你喜欢
  • 1970-01-01
  • 2015-02-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-24
  • 2022-12-16
  • 2021-09-17
  • 2021-05-30
  • 1970-01-01
相关资源
最近更新 更多