这是我的程序,用于包含大量数据(如约3315个字)的文件中的字数统计。我不想使用哈希映射。我已经使用哈希映射并获得输出,但我的任务是找到另一种方法来查找两个程序之间的频率和执行时间差异(即使用哈希映射和程序的程序没有一个)。我必须使用哈希映射或集以外的任何东西。无输出...无法理解错误
请帮忙..
在此先感谢。
package thirdassignments;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.SortedSet;
import java.util.TreeSet;
public class WordFreq2 {
public void Working()
{
FileInputStream in = null;
try {
in = new FileInputStream("C:/Users/kishansr/Desktop/file1.txt");
}
catch (FileNotFoundException ex) {
System.err.println("can’t open ");
System.exit(1);
}
String word[]=new String[100000];
int count[]={0},count1=0;
Scanner input = new Scanner(in);
//map<String,Integer> freq = new HashMap<String,Integer>();
while (input.hasNext()) {
count1=count1+1;
}
System.out.println(" Count: "+ count1);
for(int i=0;i<=count1;i++)
{
String word1 = input.next().toLowerCase();
System.out.println("word1 : " +word);
if(word[i] != word1)
{
word[i]=word1;
count[i]=1;
}
else if(word[i]==word1)
{
count[i]=count[i]+1;
}
}
for (int i=0;i<count1;i++) {
System.out.println(count[i] + " : " + word[i]);
}
}
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+" ,difference time: "+(endruntime - startruntime)+" nano seconds ");
}
}
您可能会忘记在while(input.hasNext()){}内调用'input.next()',这就是为什么它被转换为**无限**循环的原因。始终在同一个循环中使用这两种方法。 – Braj 2014-10-29 11:39:44
谢谢...明白了.. – 2014-10-29 15:44:42