import java.util.*;
public class StringSort
{
public static void main(String[] args)
{
String[] str = new String[7];
str[0] = new String("George");
str[1] = new String("Albert");
str[2] = new String("Tina");
str[3] = new String("Xavier");
str[4] = new String("Roger");
str[5] = new String("Tim");
str[6] = new String("William");
TreeSet treeSet = new TreeSet();
for (int i = 0; i < 7; i++) {
treeSet.add(str[i]);
}
Iterator it = treeSet.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
能排序 哈哈哈 不过底层是sun给解决了
数组转换为List,然后调用Java中集合的Collections.sort(list)方法
public class aaa {
public static void main(String []args){
String [] s = {"George",
"Albert",
"Tina",
"Xavier",
"Roger",
"Tim",
"William"};
String t;
for(int i = 0;i<7;i++){
for(int j=i;j<7;j++){
if(s[i].compareTo(s[j])>0){
t = s[i];
s[i]=s[j];
s[j]=t;
}
}
}
for(int i = 0; i < 7;i++)
System.out.println(s[i]);
}
}