[This is my own study notes, welcome reprint, but please specify the source:http://blog.csdn.net/jesson20121020]
continue to the last section, to the present, in fact, has been very good to achieve the various types of arrays to sort, and, extensibility is good, we define the comparator interface, can adapt to different comparison strategies, but we note that in when implementing the Compare () method, we need to move down, convert the type of object to a specific type, or feel a bit of a hassle, is there a way to specify the type we want to compare when implementing the comparator interface? At this point, it is easy to think of the <T> generics in the JDK to achieve this. So, we'll apply the <T> generics to our comparators, and we'll apply the JDK comparator and comparable to our program, as follows:
Cat.java
Import java.util.comparator;/** * Cat class has attribute height, weight and ToString () method * * @author Jesson * */public class Cat implements Java.lan g.comparable<cat> {private int height;//height private int weight;//Weight//private Comparator Comparator = new Catheight Comparator (); Height Comparator private comparator<cat> Comparator = new Catweightcomparator (); Weight Comparator public Comparator getcomparator () {return Comparator;} public void Setcomparator (Comparator Comparator) {this.comparator = Comparator;} public Cat (int height, int weight) {//TODO auto-generated constructor stubthis.height = Height;this.weight = weight;} public int getheight () {return height;} public void setheight (int height) {this.height = height;} public int getweight () {return weight;} public void setweight (int weight) {this.weight = weight;} /** * Override ToString () method */@Overridepublic String ToString () {return this.getheight () + "|" + This.getweight ();} /** * Implement comparable interface CompareTo method * Call specific comparator to compare size */@Overridepublic int compareTo (Cat o) {return comparAtor.compare (this, o);}} Catheightcomparator.java
Import java.util.comparator;/** * Cat type Height Comparator * * @author Jesson * */public class Catheightcomparator implements C omparator<cat> {@Overridepublic int compare (cat O1, cat O2) {//TODO auto-generated method Stubif (O1.getheight () &G T O2.getheight ()) return 1;else if (O1.getheight () < O2.getheight ()) Return-1;elsereturn 0;}}
Catweightcomparator.java
Import java.util.comparator;/** * Cat type Weight Comparator * * @author Jesson * */public class Catweightcomparator implements C omparator<cat> {@Overridepublic int compare (cat O1, cat O2) {//TODO auto-generated method Stubif (O1.getweight () &G T O2.getweight ()) Return-1;else if (O1.getweight () < O2.getweight ()) return 1;elsereturn 0;}}
Datasorter
Import java.lang.comparable;/** * Sort class * * @author Jesson * */public class Datasorter {/** * Bubble Sort Method * * @param cat Type Array * /public static void Bubblesort (object[] a) {for (int i = a.length-1, I >= 1; i--) {for (int j = 0; J < i; J + +) {Co Mparable O1 = (comparable) a[j]; Comparable O2 = (comparable) A[j + 1];if (O1.compareto (O2) = = 1) {Swap (A, J, J + 1);}}} /** * Bubble Sort Method * * @param a * integer array */public static void Bubblesort (int[] a) {for (int i = a.length-1; I >= 1; i--) {for (int j = 0; J < i; J + +) {if (A[j] > a[j + 1]) {Swap (A, J, J + 1);}}} /** * Exchange Two data * * @param A * array of type Object * @param x * Array subscript 1 * @param y * Array subscript 2 */private s tatic void Swap (object[] A, int x, int y) {//TODO auto-generated method Stubobject temp = a[x];a[x] = a[y];a[y] = temp;} /** * Exchange Two data * * @param A * array * @param x * Array subscript 1 * @param y * array subscript 2 */private static VO ID swap (int[] A, int x, int y) {//TODO auto-generated method Stubint temp = a[x];a[x] = a[y];a[y] = temp;} /** * Print Array * * @param a * Object type array */public static void print (object[] a) {for (int i = 0; i < a.length; i++) System.out.print (A[i] + ""); System.out.println ();} /** * Print Array * * @param a * int type array */public static void print (int[] a) {for (int i = 0; i < a.length; i++) Sy Stem.out.print (A[i] + ""); System.out.println ();}}Test.java
/** * Test class * @author Jesson * */public class Test {public static void main (string[] args) {//int[] a = new int[]{9,8,2,4,5, 6,7}; Cat[] A = {new Cat (5,5), New Cat (UP), New Cat (3,3)};//dog[] A = {new Dog (3), New Dog (2), New Dog (6)};D Atasorter.print (a);//da Tasorter.bubblesort (a); Java.util.Arrays.sort (a);D atasorter.print (a);}
As can be seen, we do not use their own definition of comparable and comparator, but with the JDK comparable<t> and comparator<t> interface, in fact, the JDK has encapsulated the sort () method, We use it directly, but there is a premise that the class that needs to be sorted implements the comparable interface of the JDK, so in the test program we can sort the arrays of our defined types directly using the sort (object[] o) method in the JDK.
Above, we write the comparable and comparator interface is only used to simulate the corresponding interface in the JDK, in the actual development process, we generally directly to use.
Java design pattern--------mode of action (3)