经过一个下午对java泛型数组的学习,通过排序程序进行总结,欢迎java大牛们批改和补充。
·java根据数组的
自然顺序对基本数据类型和String类型的泛型数组(数组也可以,不过这里只介绍泛型数组)有很好的排序功能,如下添加4条字符串,然后通过对泛型数组列表进行相应的操作,分别按照升序和降序输出:
import java.util.ArrayList;
import java.util.Collections;


public class Sort
{

public static void main(String args[])
{
ArrayList<String> array = new ArrayList<String>();
array.add("ccccc");
array.add("bbbbb");
array.add("ddddd");
array.add("aaaaa");
Collections.sort(array);
System.out.println("升序排列的结果:");
for(String str:array)
System.out.println(str);
Collections.reverse(array);
System.out.println("降序排列的结果:");
for(String str:array)
System.out.println(str);
}
}
输出结果为:
升序排列的结果:
aaaaa
bbbbb
ccccc
ddddd
降序排列的结果:
ddddd
ccccc
bbbbb
aaaaa
而通过以下
指定比较器也是可以实现的:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;


public class Sort1 implements Comparator<String>
{
private final static int UP = 1;
private final static int DOWN = -1;
private int state;


public Sort1(int state)
{
this.state = state;
}


public int compare(String o1, String o2)
{
return (state == Sort1.DOWN)? sortDown(o1, o2): sortUp(o1, o2);
}


private int sortUp(String o1, String o2)
{

if (o1.compareTo(o2) < 0)
{
return -1;

} else if (o1.compareTo(o2) > 0)
{
return 1;

} else
{
return 0;
}
}


private int sortDown(String o1, String o2)
{

if (o1.compareTo(o2) > 0)
{
return -1;

} else if (o1.compareTo(o2) < 0)
{
return 1;

} else
{
return 0;
}
}


public static void main(String[] args)
{
ArrayList<String> array = new ArrayList<String>();
array.add("ccccc");
array.add("bbbbb");
array.add("ddddd");
array.add("aaaaa");
//sort(List<T> list, Comparator<? super T> c) 根据指定比较器产生的顺序对指定列表进行排序
Collections.sort(array, new Sort1(Sort1.UP));
System.out.println("升序排列的结果:");

for (String str : array)
{
System.out.println(str);
}
Collections.sort(array, new Sort1(Sort1.DOWN));
System.out.println("降序排列的结果:");

for (String str : array)
{
System.out.println(str);
}
}
}
其输出结果同上。而当我们需要处理的是对象数组(非String)时,数组的自然序列我们是不知道的,所以,需要使用Comparator进行整体排序(关于Comparator请参见jdk-api 1.6),实现Comparator接口,便要实现其中的
compare(T o1, T o2)
。以下程序是进行比较名字,按照lastName优先,firstName其次的原则。
import java.util.*;


public class Sort2
{

public static void main(String[] args)
{
ArrayList<Name> name = new ArrayList<Name>();
name.add(new Name("John", "Lennon"));
name.add(new Name("Karl", "Marx"));
name.add(new Name("Groucho", "Marx"));
name.add(new Name("Oscar", "Grouch"));
Collections.sort(name, new Name(new String(), new String()));
for (Name i : name)
System.out.println(i.toString());
}
}


class Name implements Comparator<Name>
{
public String firstName, lastName;


public Name(String firstName, String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}


public int compare(Name n1, Name n2)
{
int Cmp = n1.lastName.compareTo(n2.lastName);
return (Cmp != 0 ? Cmp : (n2.firstName).compareTo(n1.firstName));
}


public String toString()
{
return firstName + " " + lastName;
}
}
输出结果为:
Oscar Grouch
John Lennon
Karl Marx
Groucho Marx
(G<L<M 当M相等时,K<G,所以得此输出)对于这种方式,我觉得应该会有更简洁的方式,尤其是对于
Collections.sort(name, new Name(new String(), new String()));
这句话,但我不知道怎样的实现会更好。