如果 othercontent 是字符串数组:
TreeSet<String> set = new TreeSet<>(Arrays.asList(othercontent));
othercontent = set.toArray(new String[0]);
for (String string : othercontent) {
System.out.println(string);
}
如果 othercontent 是 String :
String othercontent = "ZZZZQQWEDDODRAABBNNNNO";
LinkedList<Character> list = new LinkedList<>();
for (Character character : othercontent.toCharArray()) {
list.add(character);
}
TreeSet<Character> set = new TreeSet<>(list);
StringBuilder builder = new StringBuilder();
for (Character character : set) {
builder.append(character);
}
System.out.println(builder.toString());
这段代码不仅可以排序,还可以删除重复项
输出 :
ABDENOQRWZ