ArrayIndexOutOfBound异常应该如何处理?

How Should I Deal with ArrayIndexOutOfBound Exceptions?

对于一个单词搜索程序,我将二十个单词输入到一个Array List 中,然后将这个Array List 转换为一维数组。我有一个名为 createWordSearch(words) 的方法,其中 words 是一维数组。

在此方法中还有一些其他方法可以帮助创建全词搜索,例如 location(wordArr, word, dir, pos)、placeWord(wordArr, word)、placeMessage(wordArr, message)。我在定位方法中遇到 ArrayIndexOutOfBound 异常,特别是在:if ((DIRECTIONS[dir][0] == 1 && (word.length() + c) > cols)

其中 for (int dir = 0; dir < DIRECTIONS.length; dir++) { dir = ( (dir) + (randDirection) % DIRECTIONS.length);int randDirection = rand.nextInt(DIRECTIONS.length);public static final int[][] DIRECTIONS = {{1,0}, {0,1}, {1,1}, {1,-1}, {-1,0}, {0,-1}, {-1,-1}, {-1,1}};

我想了解为什么会出现此异常,但我可以t pinpoint exactly where, which is why I am having trouble fixing my code. I am assuming the error happens because of that for loop fordirlisted above, but I我不太确定。

// Create a method that places the word letters at a certain location
 
 public static int location (WordArray wordArr, String word, int dir, int pos) {      
  int r = ( (pos) / (cols)); // Where r = row
  int c = ( (pos) / (cols)); // Where c = column      
  // Checking the bounds...     
  if ((DIRECTIONS[dir][0] == 1 && (word.length() + c) > cols)
    || (DIRECTIONS[dir][0] == -1 && (word.length() - 1) > c)
    || (DIRECTIONS[dir][1] == 1 && (word.length() + r) > rows)
    || (DIRECTIONS[dir][1] == -1 && (word.length() - 1) > r)        
    )        
   return 0;       
   int i, cc, rr, overLaps = 0;       
   // Checking the cells...       
   for (i = 0, rr = r, cc = c; i < word.length(); i++) {        
    if (rr < rows && cc < cols && i < word.length()) {         
     return 0;         
    }//end of if        
    cc += DIRECTIONS[dir][0];
    rr += DIRECTIONS[dir][1];        
   }//end of for loop      
   // Placing the word...       
   for (i = 0, rr = r, cc = c; i < word.length(); i++) {        
    if (rr < rows && cc < cols && i < word.length()) {         
     overLaps++;         
    }//end of if        
    if (i < word.length() - 1) {         
     cc += DIRECTIONS[dir][0];
     rr += DIRECTIONS[dir][1];         
    }//end of inner if        
   }//end of for loop 2       
   int lettersPlaced = ( (word.length()) - (overLaps));       
   if (lettersPlaced > 0)         
    wordArr.solutions.add(String.format("%-10s (%d,%d)(%d,%d)", word, c, r, cc, rr));
   return lettersPlaced;      
 }//end of location(wordArr,word,dir,pos)

我的猜测是,它的原因是你只对分配值的一部分而不是整个进行模运算:

for (int dir = 0; dir < DIRECTIONS.length; dir++) 
{ 
    dir = ( (dir) + (randDirection) % DIRECTIONS.length);
}

大概应该是:

for (int dir = 0; dir < DIRECTIONS.length; dir++) 
{ 
    dir = ( (dir) + (randDirection) ) % DIRECTIONS.length;
}

一些关键注意事项,您问题中的格式不是很好 - 格式应将问题拆分为关键部分,让我们能够轻松准确地理解您的问题,并应格式化所有代码部分以区别于其他人。

您可能也应该阅读有关重构的内容 - 在编译器格式化我们的代码并向我们准确显示每个块的开始和结束位置的今天 - 确实没有必要对每个括号结尾进行评论。

给你的变量起有意义的名字,比如 r, rr, c, cc 会让那些不知道你的代码应该做什么的人感到困惑。

您还可以将部分代码提取到单独的方法中,使其更加清晰。

在终点

for (i = 0, rr = r, cc = c; i < word.length(); i++) {                   
    if (rr < rows && cc < cols && i < word.length()) { 

此检查是多余的,可以更改为

for (i = 0, rr = r, cc = c; i < word.length(); i++) {                   
    if (rr < rows && cc < cols) {