s******d 发帖数: 61 | 1 我的返回结果是这个,不知道哪里错了
d:1e:1h:1l:1o:1r:1w:1
import java.util.Collection;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;
public class Findfrequency {
public String find(String s){
char[] ch=s.toCharArray();
Hashtable hash=new Hashtable();
for(int i=0;i
Character temp=new Character(ch[i]);
if(!hash.contains(temp)){
hash.put(temp, new Integer(1));
}
else{
Integer in=hash.get(temp);
in++;
hash.put(temp, new Integer(in));
}
}
Vector v=new Vector(hash.keySet());
Collections.sort(v);
Iterator it=v.iterator();
String str="";
while(it.hasNext()){
Character element=(Character)it.next();
int a=hash.get(element).intValue();
str+=element+":"+a;
}
return str;
}
public static void main(String[] args){
Findfrequency f=new Findfrequency();
System.out.println( f.find("hello world"));
}
} | h*********n 发帖数: 915 | 2 you should debug yourself to get improved.
【在 s******d 的大作中提到】 : 我的返回结果是这个,不知道哪里错了 : d:1e:1h:1l:1o:1r:1w:1 : import java.util.Collection; : import java.util.Collections; : import java.util.Hashtable; : import java.util.Iterator; : import java.util.Vector; : public class Findfrequency { : public String find(String s){ : char[] ch=s.toCharArray();
|
|