Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正在发生IndexOutofBoundException_Java_Indexing_Indexoutofboundsexception - Fatal编程技术网

Java 正在发生IndexOutofBoundException

Java 正在发生IndexOutofBoundException,java,indexing,indexoutofboundsexception,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

我不知道为什么会出现异常。在循环之前,已分配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));
    }
 }

如果
count1
words
数组的长度,那么最后一个有效索引是
count1-1
,但是您的
for
使用
word1
在第一个循环中是空的,因此抛出
java.lang.indexoutboundsException:

在更正后,它也会显示相同的异常。
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)