【问题标题】:My java program is giving me an IndexOutOfBoundsException我的 java 程序给了我一个 IndexOutOfBoundsException
【发布时间】:2014-05-15 07:00:13
【问题描述】:

当我的程序进入我的代码的这一部分时,它会崩溃并产生这个错误

public static boolean Search(ArrayList<String> ArrayToSearch,String word)
{
    String temp;
    boolean found = false;
    for(int counter = 0;found || counter < ArrayToSearch.size();counter++)
        {
        temp = ArrayToSearch.get(counter);
        if(temp.equals(word.toLowerCase()))
        {
            found = true;
            position = counter;
        }
    }
    return found;
}

ArrayToSearch 是不同的数组列表,每行包含一个单词,表示字典。 Word 是用户想要搜索的单词。这是它产生的错误。 Add 是一个调用此方法并从中接收布尔值的方法

D:\>java Evan
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 109680, Size: 109680
        at java.util.ArrayList.rangeCheck(Unknown Source)
        at java.util.ArrayList.get(Unknown Source)
        at Evan.Search(Evan.java:95)
        at Evan.Add(Evan.java:68)
        at Evan.main(Evan.java:53)

D:\>

【问题讨论】:

  • 建议:为什么不用ArrayToSearch.indexOf(word)
  • 代码风格提示:不要将变量的第一个字符大写,使用arrayToSearch而不是ArrayToSearch开头的大写字母表示class

标签: java runtime-error indexoutofboundsexception


【解决方案1】:

查看停止条件found || counter &lt; ArrayToSearch.size()。如果你找到了需要的元素怎么办?在这种情况下,found 将是 true 并且 stop-condition 将始终是 true,您的循环将永远不会停止。正确的循环如下:

public static boolean Search(ArrayList<String> ArrayToSearch,String word)
{
    String temp;
    for(int counter = 0;counter < ArrayToSearch.size();counter++)
    {
        temp = ArrayToSearch.get(counter);
        if(temp.equals(word.toLowerCase()))
        {
            position = counter;
            return true;
        }
    }
    return false;
}

【讨论】:

    【解决方案2】:

    for() 的圆圈错误 for(int counter = 0;found || counter

    问题是当 found 为 false 并且 count>ArrayToSearch.size() 你会有 IndexOutOfBoundsException

    【讨论】:

      【解决方案3】:

      问题是你的for-loop。如果找到这个词你需要跳出循环

      public static boolean Search(ArrayList<String> ArrayToSearch,String word) {
          String temp;
          boolean found = false;
          for(int counter = 0; counter < ArrayToSearch.size(); counter++) {
            temp = ArrayToSearch.get(counter);
            if(temp.equals(word.toLowerCase())){
              position = counter;
              break; //found
            }
          }
          return found;
        }
      

      【讨论】:

        【解决方案4】:

        您的代码:

                  for(int counter = 0;found || counter < ArrayToSearch.size();counter++)
        

        像这样写:

                 for(int counter = 0;counter < ArrayToSearch.size();counter++)
        

        【讨论】:

          【解决方案5】:

          这就是问题所在:

          found || counter < ArrayToSearch.size()
          

          如果found 永远为真,那将继续永远 - 或者更确切地说,直到它因为这个异常而爆炸。我怀疑你的意思

          !found && counter < ArrayToSearch.size()
          

          换句话说:“在我们还没有找到这个词的时候继续前进,还有更多的收藏品需要浏览。”

          但是,一旦找到结果就直接返回会更清楚。如果您使用增强的 for 循环,它也会更简单:

          // Names change to follow conventions
          public static boolean search(List<String> list, String word) {
              // TODO: Handle the possibility of anything being null
              for (String candidate : list) {
                  if (candidate.equals(word)) {
                      return true;
                  }
              }
              return false;
          }
          

          或者更简单,只需使用 List.contains 即可。

          【讨论】:

            猜你喜欢
            • 2014-06-06
            • 1970-01-01
            • 2016-03-26
            • 2014-04-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多