比较器(Comparable)
一、Java常用类库比较器定义
对于比较器首先给出一个明确的定义:基本上只存在于原理分析上,而在实际的开发之中的使用意义不大。
但它依然作为一个重要的知识要掌握。
如基本比较器:Comparable
二、对象数组排序
1、Arrays类的对象数组排序方法
如果要想进行数组的操作,很明显Arrays类可以提供很好的支持,但是Arrays这个类里边有这样一个方法:
- 对象数组排序:public static void sort (0bject[] a);
操作:
打开Java Platform SE 8,选择目录Java.util.concurrent.找到 static void 栏目对应超链接 sort(0bject[] a) ,
点击打开对应文档:
public static void sort(object[] a)
Sorts the specified array of objects into ascending order, according to the natural ordering of its elements. All elements in the array must implement the Comparable interface. Furthermore, all elements in the array must be mutually comparable (that is, el.compareTo(e2) must not throw a ClassCastException for any elements el and e2 in the array).
This sort is guaranteed to be stable: equal elements will not be reordered as a result ofthe sort.……
2、示例操作:
打开simplework- Java EE ,选择Project菜单,
在*TestDemo.java 对话框输入:
package cn. mldn. demo;
class Person{
private String name ;
private int age ;
public Person(String name, int age) {
super();
this. name = name;
I this.age = age;
}
@override
public String tostring() {
return " Person[ name=" + name +",age="+ age + "]\n";
}
}
public class TestDemo {
public static void main(String[] args) throws Exception {
Person per[] = new Person[]{
new Person(' 张三”,20) ,
new Person( "李四",19) ,
new Person("王五" ,21)} ; //对象数组
Arrays.sort(per); // 要进行对象数组的排序处理
System.out.print1n(Arrays.toString(per));|
}
}
这时候,执行程序。
出现一个错误叫
“Person cannot be cast to java. Lang.Comparable”
这个错误在上边public static void sort(object[] a)文档中有提到:
在数组中的所有元素都必须实现比较接口。
此外,数组中的所有元素必须是相互可比较的(即,e1.Compareto(e2)不能对任何元素el和e2抛出
ClassCastException。
需要根据其自然元素的顺序将指定的对象数组按升序排序。
- 对象数组排序分析
1、分析
Person类里边只存有属性类别。
如:-name:String、-age:int是两个不同的属性,这里Arrays跟Person之间无法直接联系,不能直接作比较出结果。
这时候就需要Comparable来联系实现。
假设对int的数据排序:
Arrays调用Arrays.sort的指令来将数组中的值作为对象联系Comparable接口,于是将数组中的每一个元素变成Comparable的接口对象,在Comparable这个接口上,有很多我们可能比较熟悉的字段,比如Date/String/Integer。
如果要用Arrays排序,就必须让Date/String/Integer这些类实现有Comparable的接口。这个接口上有int compareTo(T o)复写。
这个时候对象所在的类必须实现Comparable接口,而Comparable接口定义如下:
public interface Comparable {
public int compareTo(T o) ;
String 类中的compareTo()就属于覆写Comparable接口所得来方法。
- 范例:实现对象数组排序
package cn. mldn. demo;
import java.util.Arrays;
class Person implements Comparble<Person> {
private String name ;
private int age ;
public Person(String name, int age) {
super();
this.name = name ;
this.age = age;
}
@override
public String tostring() {
return"Person [name=" + name + ",age=" + age + "]\n";
}
@override
public int compare To(Person o) {
// TODO Auto-generated method stub
if (this.age > o.age) {
return 1 ; // 大于
} else if (this.age < o.age) {
return -1 ;
} else {
return 0 ;
}
}
}
public class TestDemo {
public static void main(String[] args) throws Exception {
Person per[]=new Person[]{
new Person("张三",20)
new Person("李四",19)
new Person("王五" ,21)} ; //对象数组
Arrays.sort(per); // 要进行对象数组的排序处理
System.out.print1n(Arrays.toString(per));|
}
这时。执行程序。:
如果要换降序,则将正1和负1换位:
public int compare To(Person o) {
if (this.age > o.age) {
return 1 ; // 大于
} else if (this.age < o.age) {
return -1 ;
} else {
return 0 ;
}
四、结论
只要是对象数组排序,就必须有Comparable接口。