黑马程序员技术交流社区
标题:
怎么在set集合中设置比较器---将字符串(重复乱序)转变为有序且不能增删字符
[打印本页]
作者:
hdy105501
时间:
2015-10-8 22:11
标题:
怎么在set集合中设置比较器---将字符串(重复乱序)转变为有序且不能增删字符
public class HomeWorko1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串");
String s = sc.nextLine();
TreeSet<String> tr = new TreeSet<>(new Mycompara());
tr.add(s);
System.out.println(tr);
fun1(s);
}
private static void fun1(String s) {
char[] ch = s.toCharArray();
char x;
for (int i = 0; i < ch.length; i++) {
for (int j = i+1; j < ch.length; j++) {
if(ch[i]>ch[j]){
x=ch[i];
ch[i]=ch[j];
ch[j]=x;
}
}
}
System.out.println("-----------");
System.out.println(ch);
}
}
[color=Red]public class Mycompara implements Comparator<String> {
public int compare(String o1, String o2) {
while(o1.equals(o2)){
return 1;
}
return o1.compareTo(o2);
}
}
复制代码
作者:
hdy105501
时间:
2015-10-8 22:12
制造比较器,求大神指点!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2