恳请各位高手帮帮忙。
谢谢大家!!
谢谢大家!!
解决方案 »
- applet问题
- ftpClient.storeUniqueFile 上传文件失败返回false 求高人指点!!
- 一道JAVA递归算法的编程题
- 推荐:MSN客服机器人(JMMRC) ,包含代码
- 怎样用java在连接finace.yahoo.com/q后查询一个sunmicrosystems quote??
- 能不能帮我重写一下flowlayout布局,实现windows(桌面或文件夹)布局,给60分,谢谢了
- Oracle存储过程如何返回一个结果集&如何获取
- 高手请帮忙!!很急的!
- 请问怎样在Applet中加上标题菜单?
- 如何提高速度-----从数据库查询出3万条数据,并对每条数据进行处理后,生成5M文件
- java程序问题??
- Comparable 接口的问题
代码自己想思路就是这样
public static void main(String[] args){
String s = "the instruction set of the Java virtual machine distinguishes its operand types using instructions intended to operate on values of specific types";
String[] word = s.split(" ");
Map<String,Integer> m = new HashMap<String,Integer>();
//用word初使化m,m中包含了所有不重复的单词
for(int j=0;j<word.length;j++){
m.put(word[j],0);
}
Set<String> set = m.keySet();
//用word中的每个单词与m中的单词比较,发现相同的就统计一次
for(int i=0;i<word.length;i++){
Iterator<String> it = set.iterator();
while(it.hasNext()){
String k = it.next();
if(word[i].equals(k)){
int c = m.get(k);
c++;
m.put(word[i],c);
}
}
}
System.out.println(m);
}
}
根据特定的分词符,如空格和单引号等,将句子分成单词组,用split就可以办到。
2.遍历split后的数组,使用正则表达式计算每个单词出现的频率。
正则表达式的.Net代码,Apache Regex类库中有类似的正则表达式代码MatchCollection 类表示非重叠匹配的序列 该集合为只读的,并且没有公共构造函数。MatchCollection 的实例是由 Regex.Matches 属性返回的。使用 Regex 类的 Matches 方法,通过在输入字符串中找到的所有匹配填充 MatchCollection。下面代码示例演示了如何将集合复制到一个字符串数组(保留每一匹配)和一个整数数组(指示每一匹配的位置)中。MatchCollection mc;
String[] results = new String[20];
int[] matchposition = new int[20];
Regex r = new Regex("abc"); //定义一个Regex对象实例
mc = r.Matches("123abc4abcd");
for (int i = 0; i < mc.Count; i++) //在输入字符串中找到所有匹配
{
results[i] = mc[i].Value; //将匹配的字符串添在字符串数组中
matchposition[i] = mc[i].Index; //记录匹配字符的位置
}
按值排序
Iterator it = list.iterator();
while(it.hasNext()){
Object o = it.next();
System.out.println(o.toString()+" "+Collections.frequency(list,o));
}
1. 读取英文文章。(StringBuffer str)
2. 读到内存当中后,需要对其进行合理截取。考虑文章1M左右地情况,而且对文章标点符号截取考虑(,.!;空格回车)while(iterator.hasNext())
iterator.next(); //取出单词之后
HashMap map = new HashMap();
//map.get(word) == null ? map.put(word,1) : map.put(word, map.get(word) + 1);His name is John. John is 16 years old His.
StringTokenizer st = StringTokenizer(str, ",.! \n");
while (st.hasMoreTokens()){
String word = st.nextToken();
//word = His
//map的key是出现地英文单词,value是出现的次数
if(map.get(word) == null)
map.put(word, 1);
else
int value = map.get(word);
map.put(word, value + 1);
}
System.out.println(map);
His=1, name=1,is=2,John=2......map.keySet set His,name,
iterator{
TreeSet<NewStudent> list = TreeSet<NewStudent>(new new Comparator() { @Override
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub
NewStudent stu1 = (NewStudent)o1;
NewStudent stu2 = (NewStudent)o2;
return stu1.compareTo(stu2);
}
});
NewSutdent ns = new NewSutdent(His, map.get(His));
set.add(ns);
}
//set 前十个集合
NewStudent() implemets Comparable{
NewStudent(String word, int times)
times
}