尽管使用了indexInBounds,但仍获取ArrayIndexOutOfBounds

时间:2014-02-24 19:10:33

标签: java arrays recursion indexoutofboundsexception backtracking

检查是否并保持程序未使用的方法无效:array IndexOutOfBounds error。我不能继续看看我的实际执行骑士之旅是否因为这个错误而起作用。

import java.util.Arrays;
class KnightsTour {

    static int the_board[][] = new int[8][8];
    int the_tour[][] = new int [8][8];
    int k,moves = 0;
    int x = 0, y = 0; 
    int z, j = 1;
    boolean tour_found, isSafe;

        //fills 2D array with 0's
        public KnightsTour()
        {
            for (int i = 0; i < 8; i++) 
                {
                 for (int r = 0; r < 8; r++) 
                    {
                            the_board[i][r] = 0;
                        }
                }
        }
        /*recursive method, checks how many moves were made if 16 were made tour finished, 
        else if not moves knight checks if the move is valid if not back tracks*/
        public void findTour(int q)
        {
            if(moves == 68)
                {
                    tour_found = true;
                }

            else move(q); 
            if(isSafe == true)
                    {
                        findTour(q++);
                        moves++;
                    }
            else
                if(isSafe == false)
                    {
                        findTour(q*(-1));
                        moves--;
                    }
        }
        //used to keep prevent arrayindexoutofbounds error
        public boolean arrayInBounds(int x, int y)
        { 
        if(x > 8 || y > 8)
                {
                    return false;
                }
            else return true;
        }
        /*move method uses switch statement to decide which move the knight should make
        based on a case number, negative case numbers back track knight. if statement checks
        if the current array element is empty and the index is inbounds*/
        public void move(int a)
        {

            switch (a)
            {
               case 1:
                if(arrayInBounds(x+2, y+1) == true){
                   if(the_board[x+2][y+1] != 0){                          
                        the_board[x+2][y+1]=j;
                            j++;
                        }
                    }
                else isSafe = false;
               case 2:
                if (arrayInBounds(x+1, y+2) == true){
                    if(the_board[x+1][y+2] != 0){               
                            the_board[x+1][y+2]=j;
                            j++;
                        }
                }
                else isSafe = false;
               case 3:
                 if(arrayInBounds(x-1, y+2) == true){
                   if(the_board[x-1][y+2] != 0){           
                            the_board[x-1][y+2]=j;
                            j++;
                        }
                 }
                else isSafe = false;
               case 4:
                if (arrayInBounds(x-2, y+1) == true){
                    if(the_board[x-2][y+1] != 0){           
                            the_board[x-2][y+1]=j;
                            j++;
                        }
                }
                else isSafe = false;
               case 5:
                if(arrayInBounds(x-2, y-1) == true){
                    if(the_board[x-2][y-1] != 0){           
                            the_board[x-2][y-1]=j;
                            j++;
                        }
                }
                else isSafe = false;
               case 6:
                if(arrayInBounds(x-1, y-2) == true){
                        if(the_board[x-1][y-2] != 0){                    
                            the_board[x-1][y-2]=j;
                            j++;
                        }
            }
                else isSafe = false;
               case 7:
                 if(arrayInBounds(x+1, y-2) == true){
                    if(the_board[x+1][y-2] != 0){          
                            the_board[x+1][y-2]=j;
                            j++;
                        }
                 }
                 else isSafe = false;
               case 8:
                if(arrayInBounds(x+2, y-1) == true){
                 if(the_board[x+2][y-1] != 0){
                            the_board[x+2][y-1]=j;
                            j++;
                        }
                }
                else isSafe = false;
               case -1:
                 the_board[x-2][y-1]=0;
                      j--;
               case -2:
                 the_board[x-1][y-2]=0;
                      j--;
               case -3:
                 the_board[x+1][y-2]=0;
                      j--;
               case -4:
                 the_board[x+2][y-1]=0;
                      j--;
              case -5:
                 the_board[x+2][y+1]=0;
                      j--;
               case -6:
                 the_board[x+1][y+2]=0;
                      j--;
               case -7:
                 the_board[x-1][y+2]=0;
                      j--;
               case -8:
                 the_board[x-2][y+1]=0;
                      j--;
                }

        }
        //for loop to display tour once found//         
        public void displayTour()
        {
            int v = 1;
            for (int i = 0; i < 8; i++) 
                {
                 for (int e = 0; e < 8; e++) 
                    {               
                                if(v % 8 == 0)
                                {
                                    System.out.print(the_board[i][e] + "\t");
                                    System.out.println("\n");
                                } 
                        else    
                            System.out.print(the_board[i][e] + "\t");
                            v++;                
                        }
                }

        }

}

2 个答案:

答案 0 :(得分:3)

您的arrayInBounds方法不正确。 8是无效索引,但您的方法会错误地将其报告为有效。此外,它不会检查0以下的无效索引。

更改您的方法以检查xy是否大于或等于 8,并检查是否小于{{} 1}}。

答案 1 :(得分:0)

你的主板是一个二维阵列,8 x 8.但是,因为数组有0个基础,你的主板真的是这样的:

00 10 20 30 40 50 60 70
01 11 21 31 41 51 61 71
02 12 22 32 42 52 62 72
03 13 23 33 43 54 63 73
04 14 24 34 44 54 64 74
05 15 25 35 45 55 65 75
06 16 26 36 46 56 66 76
07 17 27 37 47 57 67 77

因此,如果允许值为8的indeces,它们实际上将位于第9个位置,因此超出范围。

因此,要查看数组是否超出范围,请检查索引是否大于或等于 8,或检查它是否大于7

或者,切换你的逻辑,以便检查它是否是 in-bounds 而不是越界。所以我建议改变你的方法:

public boolean arrayInBounds(int x, int y) { 
    if(x > 8 || y > 8) {
        return false;
    } else {
        return true;
    }
}

到此:

public boolean arrayInBounds(int x, int y) {
    return (x < 8 && y < 8 && x >= 0 && y >= 0);
}