Java 字频计算中的ArrayIndexOutOfBoundsException

Java 字频计算中的ArrayIndexOutOfBoundsException,java,exception,indexoutofboundsexception,Java,Exception,Indexoutofboundsexception,这是我的词频逻辑。我不应该使用HashMap来存储单词的频率。我收到一个数组索引OutofBoundsException,但不知道为什么 节目: package thirdassignments; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.Scanner; public class Wor

这是我的词频逻辑。我不应该使用
HashMap
来存储单词的频率。我收到一个
数组索引OutofBoundsException
,但不知道为什么

节目:

package thirdassignments;

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

public class WordFreq2 {

    public void Working() {
        try {
            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);
            }

            String word1[] = new String [100000];
            int count[] = {0}, 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 (word1[i] != word) {
                        word1[i] = word;
                        count[i] = 1; // here the exception is oocuring
                    }

                    else if (word1[i] == word) {
                        count[i] = count[i] + 1;
                    }
                }
            }
            for (int i = 0 ; i < count1 ; i++) {
                System.out.println(count[i] + " : " + word1[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));
    }

}
包第三次分配;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.Scanner;
公共类WordFreq2{
公共工作{
试一试{
File File=新文件(“C:/Users/kishansr/Desktop/file1.txt”);
FileReader FileReader=新的FileReader(文件);
BufferedReader BufferedReader=新的BufferedReader(文件阅读器);
StringBuffer StringBuffer=新的StringBuffer();
弦线;
而((line=bufferedReader.readLine())!=null){
stringBuffer.append(行);
stringBuffer.append(“\n”);
}
fileReader.close();
String语句=stringBuffer.toString();
String[]words=句子。拆分(\\s+);//按空格拆分
for(字符串字:字){
System.out.println(word);
}
字符串字1[]=新字符串[100000];
int count[]={0},count1=0;
for(字符串字:字){
count1=count1+1;
}
System.out.println(“计数:+count1”);
for(字符串字:字){
对于(int i=0;i
输出:




休利特
帕卡德
公司
.

公司

扩散
超过

世界


已建立
它是
足迹

几乎
全部
国家
.


a
巨大的
员工
计数


更多
女性
员工

男性
员工
.
计数:39
线程“main”java.lang.ArrayIndexOutOfBoundsException中出现异常异常:1

您的计数数组:

int count[]={0};
只有一个元素

因此,对于任何i>0,您都会得到count[i]的异常

也许您应该将其初始化为与
word1
数组相同的长度:

int count[]= new int[100000];

此外,将
word1[i]==word
替换为
word1[i].equals(word)

您已实例化大小为1的
count[]
数组。它需要至少与您的阵列一样大

试着换一行

 String word1[]=new String[100000];
 int count[]={0},count1=0;
 for (String word : words) {
     count1=count1+1;
 }


count的大小为1。因此,索引0上只有一个元素。我以为它会用0初始化数组并向其中添加元素。不知道数组大小也会通过此初始化得到修复。谢谢。如果您想这样做,请尝试改用
ArrayList
 String word1[]=new String[100000];
 int count1=0;
 for (String word : words) {
     count1=count1+1;
 }
 count[]= new int[count1];