下面是一个统计单词出现次数的例子,用到了字符串切割。但这里面为什么这样切割,小弟有点不懂,求解答。
package test;
import java.util.*;
import java.util.Map.*;
public class Test {
public static void main(String[] args){
String text = "Having a good day. Have a good class." +
"Having a good visit. Have fun!";
Map<String,Integer> treeMap = new TreeMap<String,Integer>();
String[] words = text.split("[ .!?]"); //这是什么意思?
for(int i=0;i<words.length;++i){
if(words.length()>1){
if(treeMap.get(words) != null){
int value = treeMap.get(words).intValue();
value ++;
treeMap.put(words,value);
}else{
treeMap.put(words,1);
}
}
}
System.out.println("Display words and their count in"+" ascending order of the words1");
System.out.println(treeMap);
}
}
|