|
5鱼币
- public class Test2
- {
- public static void main(String[] args)
- {
- String file ="King Athamus of northern Greece had two children," +
- "Phrixus and Helle.After he left his first wife and mar " +
- "ried Ino,a wicked woman,the two children received all" +
- "At one timethe kingdom was ruined by a famine.";
- String[] words = new String[10000];
- StringBuffer tempWord = new StringBuffer();
- int[] times = new int[100];
- String[] word = new String[100];
- String vocabulary1 = new String();
- String vocabulary2 = new String();
- int index = 0;
- int i = 0;
- for (int j = 0; j<file.length();j++)
- {
- i=j;
- char c;
- while (((c = file.charAt(i)) != ' ')
- &&((c = file.charAt(i)) != ','
- &&((c = file.charAt(i)) != '.')))//charArt返回当前位置的字符 从0开始
- {
- tempWord.append(c);
- i++;
- }
- j=i;
- System.out.print(i);
- System.out.print(" ");
- String temp = tempWord.toString();
- // System.out.println(temp);
- words[j] = temp;
- System.out.println(words[j]);
- tempWord.delete(0,tempWord.length());
- }
- System.out.println("ending");
- System.out.println(words);
- for (int j = 0; j<50;j++)
- {
- for(i = 0;i<=index;i++)
- {
- vocabulary1 = words[j];
- vocabulary2 = word[i];
- if(vocabulary1.equalsIgnoreCase(vocabulary2))
- {
- times[i] = times[i] + 1;
- break;
- }
- else if (i == index)
- {
- if(vocabulary1.equalsIgnoreCase(vocabulary2))
- {
- times[i] = times[i] + 1;
- break;
- }
- else
- {
- index += 1;
- word[index] = words[j];
- times[index] += 1;
- }
- }
- }
- }
- System.out.println(word);
- for(i = 0;i<50;i++)
- {
- System.out.println(times[i]);
- }
- }
- }
复制代码
刚学Java,有个统计词频的东西要写,呃用数组来统计的,求大佬指点,一直没闹懂哪出问题了
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class Test4 {
public static void main(String[] args) {
String file = "King Athamus of northern Greece had two children , " +
"Phrixus and Helle . After he left his first wife and mar " +
"ried Ino , a wicked woman , the two children received all" +
"At one timethe kingdom was ruined by a famine .";
String[] s1 = file.split(" ");
Map map = new LinkedHashMap();
int count = 0;
for (String s2 : s1) {
if (!s2.equals(".") && !s2.equals(",") && !s2.equals(" ")) {
//判断指定的Key是否存在
if (map.containsKey(s2)) {
//获取当前单词次数count
count = (Integer) map.get(s2);
//次数加1
map.put(s2, ++count);
} else {
count=1;
map.put(s2, count);
}
}
}
Iterator it = map.keySet().iterator();
String word;
while (it.hasNext()) {
word = (String) it.next();
System.out.println(word + "------->" + map.get(word));
}
}
}
|
最佳答案
查看完整内容
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class Test4 {
public static void main(String[] args) {
String file = "King Athamus of northern Greece had two children , " +
"Phrixus and Helle . After he left his first wife and mar " +
"ried Ino , a wicked woman , the two children received all" +
...
|