这是我的字数统计程序,在一个包含大量数据的文件中,大约有3315个单词。我不想使用散列映射.I已经使用了散列映射并得到了输出,但我的任务是找到另一种方法来找到两个程序之间的频率和执行时间差(即有散列映射的程序和没有散列映射的程序)。我必须使用除散列映射或集合之外的任何方法。
请帮帮忙..
提前谢谢。
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 ");
}
}发布于 2014-10-29 19:50:32
使用正则表达式模式匹配,获取第一个单词,将单词与剩余字符串进行匹配并统计其数量,打印计数并将其替换为空白,重复此过程即可完成所有单词。
https://stackoverflow.com/questions/26629369
复制相似问题