IndexOutofBoundException正在发生

时间:2014-11-06 00:13:35

标签: java indexing indexoutofboundsexception

我无法弄清楚异常的原因。在我们循环之前,分配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)

2 个答案:

答案 0 :(得分:0)

如果count1words数组的长度,则最后一个有效索引为count1-1,但您的for使用<= count1来查找words[count1] {1}}在界外。将<=变为<

在任何情况下都无需手动计算阵列的长度,它已经可用words.length

答案 1 :(得分:0)

word1在第一个循环中为空,因此抛出java.lang.IndexOutOfBoundsException: