ArrayIndexOutOfBoundsException:-1

时间:2014-11-24 23:17:40

标签: java arrays indexoutofboundsexception

所以我正在编写一个带有2D数组的代码,就像一个方形表一样排列10比10。它充满了X和Os以及空白。输入阈值,如果X或O周围的指数百分比也是X或Os大于阈值而不是满足点,如果不满足则不满足。我已经能够打印好表,但后来我尝试做了令人满意的部分,我得到了一个超出范围的数组索引异常。我知道这意味着什么,但不知道如何使我的代码正常工作,虽然我担心我将不得不重新设计它。另一件事是,我不确定我是否正确地做了布尔。

public class CellSim{

public static void main(String[] args){
    char [][] tissue = new char [10][10];
    int threshold = 30;
    assignCellTypes(tissue, 50, 25);
    printTissue(tissue);
    System.out.println();

    boolean boardSat = true;
    boardSat = boardSatisfied(tissue, threshold);

    if( boardSat == false){
        System.out.println( "board is not satisfied");}
    if( boardSat == true){
        System.out.println("board is satisfied");}



}


public static void printTissue(char[][] tissue){
    for(int row = 0;row < tissue.length;row++){
        for(int col = 0;col < tissue[row].length;col++){
            System.out.print(tissue[row][col] + "\t");
        }
        System.out.println();
    }
}


public static void assignCellTypes(char[][] tissue, int percentBlank, int percentX){
int n = (tissue.length) * (tissue.length);
percentBlank = (int) Math.ceil(n * (percentBlank * .01));
percentX = (int) Math.ceil((n - percentBlank) * (percentX * .01));
int percentO = (int) Math.ceil(n - percentBlank - percentX);

for( int i = 0; i < percentBlank; i++){
while(percentBlank > 0){
    int randCell = randInt(0, 9);
    int randCell2 = randInt(0, 9);
                if(tissue[randCell][randCell2] == '\u0000'){
                    tissue[randCell][randCell2] = ' ';
                    break;
                    }
}
}
for( int i = 0; i < percentX; i++){
while(percentX > 0){
    int randCell = randInt(0, 9);
    int randCell2 = randInt(0, 9);
                if(tissue[randCell][randCell2] == '\u0000'){
                    tissue[randCell][randCell2] = 'X';
                    break;
                    }
}
}
for( int i = 0; i < percentO; i++){
while(percentO > 0){
    int randCell = randInt(0, 9);
    int randCell2 = randInt(0, 9);
                if(tissue[randCell][randCell2] == '\u0000'){
                    tissue[randCell][randCell2] = 'O';
                    break;
                    }
}
}

}
public static boolean isSatisfied(char[][] tissue, int row, int col, int threshold){
    int total = 0;
    int same = 0;
    if(tissue[row][col] == 'X'){
        total = 0;
            if(tissue[row + 1][col - 1] == 'X'){
                same ++;
                total ++;
            }else if(tissue[row + 1][col - 1] == 'O')
                total ++;
            if(tissue[row + 1][col] == 'X'){
                same ++;
                total ++;
            }else if(tissue[row + 1][col] == 'O')
                total ++;
            if(tissue[row + 1][col + 1] == 'X'){
                same ++;
                total ++;
            }else if(tissue[row + 1][col + 1] == 'O')
                total ++;
            if(tissue[row][col - 1] == 'X'){
                same ++;
                total ++;
            }else if(tissue[row][col - 1] == 'O')
                total ++;
            if(tissue[row][col + 1] == 'X'){
                same ++;
                total ++;
            }else if(tissue[row][col + 1] == 'O')
                total ++;
            if(tissue[row - 1][col - 1] == 'X'){
                same ++;
                total ++;
            }else if(tissue[row - 1][col - 1] == 'O')
                total ++;
            if(tissue[row - 1][col] == 'X'){
                same ++;
                total ++;
            }else if(tissue[row - 1][col] == 'O')
                total ++;
            if(tissue[row - 1][col + 1] == 'X'){
                same ++;
                total ++;
            }else if(tissue[row - 1][col + 1] == 'O')
                total ++;

    }
    if(tissue[row][col] == 'O'){
        total = 0;
            if(tissue[row + 1][col - 1] == 'O'){
                same ++;
                total ++;
            }else if(tissue[row + 1][col - 1] == 'X')
                total ++;
            if(tissue[row + 1][col] == 'O'){
                same ++;
                total ++;
            }else if(tissue[row + 1][col] == 'X')
                total ++;
            if(tissue[row + 1][col + 1] == 'O'){
                same ++;
                total ++;
            }else if(tissue[row + 1][col + 1] == 'X')
                total ++;
            if(tissue[row][col - 1] == 'O'){
                same ++;
                total ++;
            }else if(tissue[row][col - 1] == 'X')
                total ++;
            if(tissue[row][col + 1] == 'O'){
                same ++;
                total ++;
            }else if(tissue[row][col + 1] == 'X')
                total ++;
            if(tissue[row - 1][col - 1] == 'O'){
                same ++;
                total ++;
            }else if(tissue[row - 1][col - 1] == 'X')
                total ++;
            if(tissue[row - 1][col] == 'O'){
                same ++;
                total ++;
            }else if(tissue[row - 1][col] == 'X')
                total ++;
            if(tissue[row - 1][col + 1] == 'O'){
                same ++;
                total ++;
            }else if(tissue[row - 1][col + 1] == 'X')
                total ++;


    }
    if(tissue[row][col] == ' '){
    return true;
    }if(total == 0){
        return false;
        }else if(((same / total) * 100) >= threshold){
        return true;
    }else{ return false;}
}       

 public static boolean boardSatisfied(char[][] tissue, int threshold){
    boolean isSat = true;
    while( isSat == true){
        for(int row = 0;row < tissue.length;row++){
            for(int col = 0;col < tissue[row].length;col++){
            isSat = isSatisfied(tissue, row, col, threshold);

            }
        }
    }
        if(isSat == false){
        return false;
        }else{return true;} 
}


public static int randInt(int min, int max){

   int range = (max - min) + 1;     
   return(int)(Math.random() * range) + min;
}



}

2 个答案:

答案 0 :(得分:1)

您应该检查row, col是&gt; = 0和&lt; tissue.size。对于row / col +/- 1也是如此。此外,您还可以通过移出total++并一起减少条件,在isSatisfied方法中重构代码。考虑在这里使用regexp。

答案 1 :(得分:0)

问题出在你的isSatisfied方法上。您不执行任何边界检查以确保您只访问阵列中的有效选项。您永远不应该尝试访问数组边界之外的值。执行此操作时,您会收到ArrayIndexOutOfBounds异常。这就是你的情况。正如您对问题的评论所建议的那样,您应该进一步调查边界检查,以便您在此处了解基本问题。

您的方法应更改为:

public static boolean isSatisfied(char[][] tissue, int row, int col, int threshold){
int total = 0;
int same = 0;
if(tissue[row][col] == 'X'){
    total = 0;
        if(col-1 >= 0 && row+1 < tissue.length && tissue[row + 1][col - 1] == 'X'){
            same ++;
            total ++;
        }else if(col-1 >= 0 && row+1 < tissue.length && tissue[row + 1][col - 1] == 'O')
            total ++;
        if(row+1 < tissue.length && tissue[row + 1][col] == 'X'){
            same ++;
            total ++;
        }else if(row+1 < tissue.length && tissue[row + 1][col] == 'O')
            total ++;
        if(row+1 < tissue.length && col+1 < tissue[row+1].length && tissue[row + 1][col + 1] == 'X'){
            same ++;
            total ++;
        }else if(row+1 < tissue.length && col+1 < tissue[row+1].length && tissue[row + 1][col + 1] == 'O')
            total ++;
        if(col-1 >= 0 && tissue[row][col - 1] == 'X'){
            same ++;
            total ++;
        }else if(col-1 >= 0 && tissue[row][col - 1] == 'O')
            total ++;
        if(col+1 < tissue[row].length && tissue[row][col + 1] == 'X'){
            same ++;
            total ++;
        }else if(col+1 < tissue[row].length && tissue[row][col + 1] == 'O')
            total ++;
        if(row-1 >= 0 && col-1 >= 0 && tissue[row - 1][col - 1] == 'X'){
            same ++;
            total ++;
        }else if(row-1 >= 0 && col-1 >= 0 && tissue[row - 1][col - 1] == 'O')
            total ++;
        if(row-1 >= 0 && tissue[row - 1][col] == 'X'){
            same ++;
            total ++;
        }else if(row-1 >= 0 && tissue[row - 1][col] == 'O')
            total ++;
        if(row-1 >= 0 && col+1 < tissue[row-1].length && tissue[row - 1][col + 1] == 'X'){
            same ++;
            total ++;
        }else if(row-1 >= 0 && col+1 < tissue[row-1].length && tissue[row - 1][col + 1] == 'O')
            total ++;

}
if(tissue[row][col] == 'O'){
    total = 0;
        if(col-1 >= 0 && row+1 < tissue.length && tissue[row + 1][col - 1] == 'O'){
            same ++;
            total ++;
        }else if(col-1 >= 0 && row+1 < tissue.length && tissue[row + 1][col - 1] == 'X')
            total ++;
        if(row+1 < tissue.length && tissue[row + 1][col] == 'O'){
            same ++;
            total ++;
        }else if(row+1 < tissue.length && tissue[row + 1][col] == 'X')
            total ++;
        if(row+1 < tissue.length && col+1 < tissue[row+1].length && tissue[row + 1][col + 1] == 'O'){
            same ++;
            total ++;
        }else if(row+1 < tissue.length && col+1 < tissue[row+1].length && tissue[row + 1][col + 1] == 'X')
            total ++;
        if(col-1 >= 0 && tissue[row][col - 1] == 'O'){
            same ++;
            total ++;
        }else if(col-1 >= 0 && tissue[row][col - 1] == 'X')
            total ++;
        if(col+1 < tissue[row].length && tissue[row][col + 1] == 'O'){
            same ++;
            total ++;
        }else if(col+1 < tissue[row].length && tissue[row][col + 1] == 'X')
            total ++;
        if(row-1 >= 0 && col-1 >= 0 && tissue[row - 1][col - 1] == 'O'){
            same ++;
            total ++;
        }else if(row-1 >= 0 && col-1 >= 0 && tissue[row - 1][col - 1] == 'X')
            total ++;
        if(row-1 >= 0 && tissue[row - 1][col] == 'O'){
            same ++;
            total ++;
        }else if(row-1 >= 0 && tissue[row - 1][col] == 'X')
            total ++;
        if(row-1 >= 0 && col+1 < tissue[row-1].length && tissue[row - 1][col + 1] == 'O'){
            same ++;
            total ++;
        }else if(row-1 >= 0 && col+1 < tissue[row-1].length && tissue[row - 1][col + 1] == 'X')
            total ++;


}
if(tissue[row][col] == ' '){
return true;
}if(total == 0){
    return false;
    }else if(((same / total) * 100) >= threshold){
    return true;
}else{ return false;}
}