【问题标题】:IndexOutofBoundException is occurringIndexOutofBoundException 正在发生
【发布时间】:2015-01-02 09:03:54
【问题描述】:

我无法弄清楚为什么会出现异常。 Count1(在程序中)在我们循环之前被赋值。

程序是一个文件中的字数统计,该文件包含39个字。

程序:

package thirdassignments;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class WordFreq2 {

ArrayList word1=new ArrayList();
 //String word1[]=new String[100000];
ArrayList<Integer> count = new ArrayList<Integer>(); 
//int count[]= new int[10000000];
 boolean wordexists = false;
 int index;
 int lastindex;
public void Working()
{

    try{
        boolean flag=false;
        File file = new File("C:/Users/kishansr/Desktop/file1.txt");
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        StringBuffer stringBuffer = new StringBuffer();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            stringBuffer.append(line);
            stringBuffer.append("\n");
        }
        fileReader.close();

        String sentence=stringBuffer.toString();
        String[] words = sentence.split("\\s+"); // splits by whitespace
        for (String word : words) {
                System.out.println(word);
        } 


     int count1=0;
     for (String word : words) {
         count1=count1+1;
     }
     System.out.println("Count :"+count1);
     for (String word : words) {

         for(int i=0;i<=count1;i++)
         {
             if(word == word1.get(i)) //Exception is occurring here              
             {
                 wordexists = true;
                 index=i;
                 break;
             }
         }

         if(wordexists==true)
         {
             int add = count.get(index)+1;
             count.set(index,add);
             wordexists=false;
         }

         if(wordexists==false)
         {
             lastindex=word1.size()+1;
             word1.set(index, word);
             count.set(index, 1);
         }
     }

      for (int i=0;i<count1;i++) {
         System.out.println(count.get(i) + " : " + word1.get(i));
     }

}catch (IOException e1) {
    e1.printStackTrace();}
}
    public static void main(String[] args) {
         // TODO Auto-generated method stub
        WordFreq2 wf = new WordFreq2();
        long startruntime = System.nanoTime();
        wf.Working();
        long endruntime = System.nanoTime();
        System.out.println("start time: "+startruntime+" end time :"+endruntime+" diferrence: "+      (endruntime - startruntime));
    }
 }

输出:

This
is
the
Hewlett
Packard
company
.
This
Company
is
spread
over
the
world
and
has
established
its
footprints
in
almost
all
countries
.
It
has
a
huge
employee
count
and
has
more
women
employees
than
male
employees
.
Count :39
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at thirdassignments.WordFreq2.Working(WordFreq2.java:50)
    at thirdassignments.WordFreq2.main(WordFreq2.java:87)

【问题讨论】:

    标签: java indexing indexoutofboundsexception


    【解决方案1】:

    word1 在第一个循环中是空的,所以抛出java.lang.IndexOutOfBoundsException:

    【讨论】:

      【解决方案2】:

      如果count1words 数组的长度,那么最后一个有效索引是count1-1,但您的for 使用&lt;= count1,它允许查找超出范围的words[count1]。将&lt;= 转为&lt;

      在任何情况下都不需要手动计算数组的长度,它已经作为words.length 提供。

      【讨论】:

      • 更正后也显示相同的异常。
      猜你喜欢
      • 2014-10-26
      • 1970-01-01
      • 1970-01-01
      • 2014-09-10
      • 2016-11-03
      • 1970-01-01
      • 2016-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多