我把字符串存到arraylist里面,遍历后再用treeset存进去保证唯一性,为什么得不出结果
public class d7 {
public static void main(String[] args) {
String str="fgdfgfgh";
sys(str);
}
private static <E> void sys(String str) {
char[] chs=str.toCharArray();
ArrayList<Character> list=new ArrayList<Character>();
TreeSet<Object> temp=new TreeSet<Object>(new ComparatorByObject());
for(int x=0;x<chs.length;x++){
list.add(chs[x]);
}
char[] chr={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
for(int x=0;x<chr.length;x++){
Object obj=chr[x];
int count=0;
while(list.contains(obj)){
count++;
list.remove(obj);
temp.add(obj);
}
Iterator<Object> it=temp.iterator();
while(it.hasNext()){
System.out.print(it.next()+":"+count+".");
}
}
}
}
class ComparatorByObject implements Comparator<Object>{
Object o1;
Object o2;
public int compare(Object o1,Object o2){
int temp=(o1.toString()).compareTo(o2.toString());
return temp;
}
} |