Comparable接口标签:Comparable,return,Comparator,package,int,接口,strategy,com,public From: https://blog.51cto.com/u_10028442/5824685
1.接口
package com.ygl.strategy;
public interfaceComparable {
public int compareTo(Object o);
}
2.Cat类实现Comparable接口,并重写compareTo方法
package com.ygl.strategy;
public classCat implementsComparable{
private intheight;
private intweight;
public intgetHeight() {
return height;
}
public voidsetHeight(intheight) {
this.height
}
public intgetWeight() {
return weight;
}
public Cat(intheight, intweight) {
super();
this.height
this.weight
}
public voidsetWeight(intweight) {
this.weight
}
@Override
public String toString() {
return "Cat [height="+ height+ ", weight=" + weight+ "]";
}
@Override
public intcompareTo(Object o) {
if(o instanceof Cat){
Catc=(Cat)o;
if(this.getHeight()>c.getHeight()){
return 1;
}
else if(this.getHeight()<c.getHeight()){
return -1;
}else
return 0;
}
return -100;//抛异常
}
}
3.定义排序规则
package com.ygl.strategy;
public classDataSorter {
public static void sort(Object[] a) {
// TODO Auto-generated method stub
for(int i=a.length;i>0;i--){//遍历几次1-n
for(int j=0;j<i-1;j++){
Comparableo1=(Comparable)a[j];
Comparableo2=(Comparable)a[j+1];
if(o1.compareTo(o2)==1){
swap(a,j,j+1);
}
}
}
}
private static void swap(Object[] a, int x ,int y) {
Objecttemp=a[x];
a[x]=a[y];
a[y]=temp;
}
public static void sort(int[] a) {
// TODO Auto-generated method stub
for(int i=a.length;i>0;i--){//遍历几次1-n
for(int j=0;j<i-1;j++){
if(a[j]>a[j+1]){
swap(a,j,j+1);
}
}
}
}
//************************************************************
private static void swap(int[] a, int x, int y) {
int temp=a[x];
a[x]=a[y];
a[y]=temp;
}
public static void p(int[] a) {
// TODO Auto-generated method stub
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
4.测试
package com.ygl.strategy;
public classTest {
public staticvoidmain(String[] args) {
int a[]={8,9,2,5};
Cat[]c={newCat(8,5),newCat(2,5)};
DataSorter.sort(c);
DataSorter.p(c);
}
}
//=============================================================
Comparator接口(推荐使用)
1.类一定要实现Comparable接口(理由略)
package com.ygl.strategy;
import java.util.Comparator;
import java.lang.Comparable;;
public classDog implementsComparable<Dog>{
private intfood;
private Comparator<Dog> comparator= newDogHeighgtComparator() ;
public Dog(intfood) {
this.food=food;
}
public Comparator<Dog> getComparator() {
return comparator;
}
public voidsetComparator(Comparator<Dog> comparator) {
this.comparator
}
public intgetFood() {
return food;
}
public voidsetFood(intfood) {
this.food
}
@Override
public String toString() {
return "Dog [food="+ food+ "]";
}
public intcompareTo(Dog o) {
returncomparator.compare(this, o);
}
}
2.自定义比较器
package com.ygl.strategy;
import java.util.Comparator;
public classDogHeighgtComparator implements Comparator<Dog> {
@Override
public int compare( Dog d1, Dogd2) {
if(d1.getFood()>d2.getFood()){
return 1;
}else if(d1.getFood()<d2.getFood()){
return -1;
}
return 0;
}
}
3.使用jdk自带排序类 Arrays.sort(d);
package com.ygl.strategy;
import java.util.Arrays;
public classTest {
public staticvoidmain(String[] args) {
int a[]={8,9,2,5};
Dog[]d={newDog(25),newDog(5)};
Arrays.sort(d);
DataSorter.p(d);
}
}