Java基础之比较器、Arrays类、Comparable接口、Comparator接口

一、Arrays这个工具类主要用于数组相关的操作功能,在这个类里面有二分查找法、比较两个数组:

  public static int binarySearch(byte[] a, byte key)//在进行此调用之前,数组必须按照sort(byte[])方法进行排序

 1     /**
 2      * 使用Arrays类实现二分查找
 3      */
 4     @Test
 5     public void changeDateWithLong(){
 6         int[] ints = new int[]{21,33,55,4,556,8,66,44,1,2};
 7         Arrays.sort(ints);
 8         /***
 9          * 二分查找,在指定的数组中查找指定的数字
10          * 如果没有找到返回-1
11          * 找到了,返回数字所在的下标索引
12          */
13         int i = Arrays.binarySearch(ints, 556);
14         System.out.println(i);
15     }
  public static boolean equals(boolean[] a,boolean[] a2)//想要两个数组相同,需数组中的元素顺序一致
  public static void fill(boolean[] a,boolean val)//填充数组
  public static String toString(int[] a)//将数组变为字符串输出
  public static void sort(Object[] a)//对元素排序,数组中所有的元素(除了基本数据类型)必须先实现Comparable接口。否则没有提供比较的规则,
  所有无法进行比较
二、Comparable<T>泛型接口接口
  int compareTo(T o);//比较此对象与指定的对象后,进行排序,返回-1小于,0等于,1大于。
BinaryTree(数据结构)
数本质上属于动态对象数组,用于对数据进行排序
操作原理,选择第一个数据作为根节点,而后比根节点小的数据放在根节点的左子树(左边),比根节点大的数据放在根节点的(右子树),取出时,按照中序
遍历方式取出(左-中-右)
示例:实现二叉树
  1、定义出要使用的数据,数据所在的类要实现Comparable接口
  2、定义二叉树,所有的数据结构都要有Node类的支持
  1 package test;
  2 
  3 import java.util.Arrays;
  4 
  5 
  6 /**
  7  * 定义出要使用的数据,数据所在的类要实现Comparable接口
  8  */
  9 class Book implements Comparable<Book>{
 10 
 11     private String title;//书名
 12     private double price;//价格
 13 
 14     public Book(String title,double price){
 15         this.title = title;
 16         this.price = price;
 17     }
 18     @Override
 19     public String toString() {
 20         return "书名:"+this.title+",价格:"+this.price+"\n";
 21     }
 22     /**
 23      * 实现接口的定义比较规则的方法
 24      * @param o
 25      * @return
 26      */
 27     @Override
 28     public int compareTo(Book o) {
 29         if (this.price > o.price){
 30             return 1;
 31         }else if (this.price < o.price){
 32             return -1;
 33         }else{
 34             return 0;
 35         }
 36     }
 37 }
 38 /**
 39  * 二叉树类实现
 40  */
 41 class BinaryTree{
 42 
 43     //    内部类  开始
 44     private class Node{
 45         /**
 46          * 排序的依据就是Comparable
 47          */
 48         private Comparable data;
 49         private Node leftNode;//保存左子树
 50         private Node rightNode;//保存右子树
 51         public Node(Comparable data){
 52             this.data = data;
 53         }
 54         public void addNode(Node newNode){
 55             if(this.data.compareTo(newNode.data) > 0){//降序
 56                 if (this.leftNode == null){
 57                     this.leftNode = newNode;
 58                 }else {//继续向下添加
 59                     this.leftNode.addNode(newNode);
 60                 }
 61             }else{
 62                 if (this.rightNode == null){
 63                     this.rightNode = newNode;
 64                 }else{
 65                     this.rightNode.addNode(newNode);
 66                 }
 67             }
 68         }
 69 
 70         /**
 71          * 输出数据方法
 72          */
 73         public void toArrayNode(){
 74             if (this.leftNode != null){//表示有左节点
 75                 this.leftNode.toArrayNode();//左子树输出
 76             }
 77             BinaryTree.this.retData[BinaryTree.this.foot++] = this.data;//保存数据
 78             if (this.rightNode != null){//表示有右节点
 79                 this.rightNode.toArrayNode();//右子树输出
 80             }
 81         }
 82     }
 83 //    内部类    结束
 84 
 85     private Node rootNode;//定义根节点
 86     private int count;//保存元素个数
 87     private Object[] retData;//标记数组数据
 88     private int foot;//标记下标
 89     /**
 90      * 进行数据的增加
 91      * @param object
 92      */
 93     public void add(Object object){
 94         //必须将其转换为Comparable才能进行数据的保存
 95         Comparable able = (Comparable) object;
 96         //示例Node对象,创建新的节点
 97         Node newNode = new Node(able);
 98         if (this.rootNode == null){//不存在根节点
 99             this.rootNode = newNode;
100         }else{//否则交给Node类的addNode方法处理
101             this.rootNode.addNode(newNode);
102         }
103         this.count++;
104     }
105     //取数据
106     public Object[] toArray(){
107         if (this.rootNode == null){
108             return null;
109         }
110         this.foot = 0;
111         //定义数组,保存数据
112         this.retData = new Object[this.count];
113         this.rootNode.toArrayNode();
114         return this.retData;
115     }
116 }
117 
118 /**
119  * 测试类
120  */
121 public class TestBinaryTree {
122     public static void main(String[] args) {
123         BinaryTree binaryTree = new BinaryTree();
124         binaryTree.add(new Book("Java开发",98.8));
125         binaryTree.add(new Book("Web前端",56.8));
126         binaryTree.add(new Book("Java设计思想",128.8));
127         binaryTree.add(new Book("Java设计思想2",18.8));
128         binaryTree.add(new Book("Java设计思想3",28.8));
129         Object[] objects = binaryTree.toArray();
130         System.out.println(Arrays.toString(objects));
131     }
132 }
三、Comparator:挽救的比较器
@FunctionalInterface
public interface Comparator<T>
  int compare(T o1,T o2)//需要单独准备一个类来实现Comparator接口,这个类作为指定类的排序类
  boolean equals(Object obj)
利用Arrays类中,重载的sort方法 比较
public static <T> void sort(T[] a, Comparator<? super T> c);//设置了泛型的下限,找到的是Book或者是其父类
 示例:实现排序 
 
 1 package test;
 2 
 3 import java.util.Arrays;
 4 import java.util.Comparator;
 5 
 6 
 7 /**
 8  * 定义出要使用的数据,数据所在的类要实现Comparable接口
 9  */
10 class Book {
11 
12     private String title;//书名
13     private double price;//价格
14 
15     public String getTitle() {
16         return title;
17     }
18 
19     public void setTitle(String title) {
20         this.title = title;
21     }
22 
23     public double getPrice() {
24         return price;
25     }
26 
27     public void setPrice(double price) {
28         this.price = price;
29     }
30 
31     public Book(String title, double price){
32         this.title = title;
33         this.price = price;
34     }
35     @Override
36     public String toString() {
37         return "书名:"+this.title+",价格:"+this.price+"\n";
38     }
39 }
40 
41 /**
42  * 专门为Book类排序的工具类
43  */
44 class BookComparator implements Comparator<Book>{
45     @Override
46     public int compare(Book o1, Book o2) {
47         if (o1.getPrice()>o2.getPrice()){
48             return 1;
49         }else if (o1.getPrice() < o2.getPrice()){
50             return -1;
51         }else {
52             return 0;
53         }
54     }
55 }
56 
57 /**
58  * 测试类
59  */
60 public class TestBinaryTree {
61     public static void main(String[] args) {
62         Book[] books = new Book[]{
63                 new Book("Java开发",98.8),
64                 new Book("Web前端",56.8),
65                 new Book("Java设计思想",128.8),
66                 new Book("Java设计思想2",18.8),
67                 new Book("Java设计思想3",28.8)
68         };
69         Arrays.sort(books,new BookComparator());//挽救比较器
70         System.out.println(Arrays.toString(books));
71     }
72 }

使用Comparator排序,需要专门定义一个排序类,且调用排序时,也要明确的指明一个排序规则类;

大多数情况下都是使用Comparable接口。

解释Comparable与Comparator的区别?

  》如果对象数组进行排序,必须要设置排序规则,可以使用Comparable或者Comaparator接口实现。

  》java.lang.Comparable在一个类定义的时候实现好的接口,这样本类的对象数组就可以进行排序,而在Comparable接口下定义有一个compareTo()方法;

  》java.util.Comparator是专门定义一个指定类的比较规则,属于挽救的比较操作,里面有两个方法,compare()与equals()

猜你喜欢

转载自www.cnblogs.com/in-the-game-of-thrones/p/11330259.html