input a string "hello word", print l:3 o:2 e:1 d:1 h:1 r:1 w:1.不知道哪错了# JobHunting - 待字闺中
s*d
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"));
}
}
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
for(int i=0;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"));
}
}