简体   繁体   English

如何修复 Java 中的“Index 5 out of bounds for length 5”错误

[英]How to fix “Index 5 out of bounds for length 5” error in Java

I am working on solution for the CCC 2018 Robo Thieves Problem, it a simplified version of the original problem.我正在研究 CCC 2018 Robo Thieves 问题的解决方案,它是原始问题的简化版本。 My problem is that it will give me this "Index 5 out of bounds for length 5" when I executed my code and I'm not sure why it is happening.我的问题是,当我执行我的代码时,它会给我这个“索引 5 超出长度 5 的范围”,我不确定它为什么会发生。 Half of my program executes and then this error occurs.我的程序执行了一半,然后发生此错误。

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

Upon first look, I expect that the issue may lie in your getLeft(), getAbove(), getRight(), and getBelow() methods.乍一看,我认为问题可能在于您的 getLeft()、getAbove()、getRight() 和 getBelow() 方法。

In these methods, you give it a value for cRow and cCol and add or subtract 1. However, you need to make sure that the index you are querying does not exceed the size of the double array factGrid , or go below 0.在这些方法中,你给它一个 cRow 和 cCol 的值并加或减 1。但是,你需要确保你查询的索引不超过双数组factGrid的大小,或者 go 小于 0。

For example, in your getRight() method, you might try:例如,在您的 getRight() 方法中,您可以尝试:

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

暂无
暂无

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

相关问题 如何修复错误ArrayIndexOutOfBoundsException:索引1超出长度1的范围? - How to fix thid error ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1? 如何在Java中纠正Arraylist索引超出长度错误的范围 - How to correct Arraylist index out of bounds for length error in Java 如何修复Java数组索引超出范围错误 - how to fix the java array index out of bounds error 如何修复数组索引超出范围的错误? - How to fix array index out of bounds error? 如何解决越界索引错误? - How to fix out of bounds index error? 如何修复ArrayList java.lang.IndexOutOfBoundsException:长度为2的索引20越界 - How to fix ArrayList java.lang.IndexOutOfBoundsException: Index 20 out-of-bounds for length 2 如何在 Spring 引导中修复“嵌套异常是 java.lang.ArrayIndexOutOfBoundsException:索引 2 超出长度 2 的范围” - ZE4728F444B24839E3F80ADF3829BCBA - How to fix "nested exception is java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2" in a Spring Boot - postgresql Project Arraylist java.lang.IndexOutOfBoundsException:索引 3 超出长度 3 错误的范围 - Arraylist java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3 Error 线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:索引 1 超出长度 0 的范围,不知道这是什么意思或如何解决 - Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 0, no idea what does that mean or how to fix it Java索引超出范围错误 - Java index out of bounds error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM