java学习之路----java类库----Arrays对对象的排序---二叉树实现原理

来源:互联网 发布:gta5捏脸贝克汉姆数据 编辑:程序博客网 时间:2024/05/22 03:37
 1.Arrays 是数组的操作类。。。主要的功能是实现数组的查找,填充,排序

          
     例子:
public class ArrayDemo {
      public static void main(String[] args) {
               int [] a={1,3,2,5,4,7,6,8,9};
              
              Arrays. sort(a); //排序
              
              System. out .println(Arrays.toString(a)); //以字符串的形式输出数组
              
          System. out .println(Arrays.binarySearch(a, 7));//检索a中7的位置
          
          Arrays. fill(a, 0); //以零来填充
          
          System. out .println(Arrays.toString(a));
          
          
     }
}

结果:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
6
[0, 0, 0, 0, 0, 0, 0, 0, 0]

2.如果想要对对象数组进行排序,则必须实现Comparable接口

     class Student implements Comparable<Student>{
     
      private String name;
     
      private int age ;
     
      private float score ;
     

      public Student(String name, int age, float score) {
           this .name = name;
           this .age = age;
           this .score = score;
     }



      @Override
      public String toString() {
          
           return this .name +"\t" + this. age+ "\t"+ this .score ;
     }



      public int compareTo(Student stu) {
                    if (this .score >stu.score ){
                         return -1;
                   } else if ( this. score<stu. score){
                         return 1;
                   } else {
                         if (this .age >stu.age ){
                              return -1;
                        } else if ( this. age<stu. age){
                              return 1;
                        } else {
                              return 0;
                        }
                   }
          
     }
     
}

public class ComparableDemo1 {
           public static void main(String[] args) {
                        Student student1= new Student("like" , 22, 90.0f);
                        Student student2= new Student("张三" , 22, 90.0f);
                        Student student3= new Student("王五" , 20, 90.0f);
                        Student student4= new Student("李四" , 22, 99.0f);
                        Student student5= new Student("小花" , 22, 89.0f);
                        
                        Student [] stu={student1,student2,student3,student4,student5};
                        Arrays. sort(stu); //进行排序
                         for (int i=0;i<stu. length;i++){
                             System. out .println(stu[i]);
                        }
          }
}

结果:
李四 22   99.0
like 22   90.0
张三 22   90.0
王五 20   90.0
小花 22   89.0


如果程序不实现Comparable接口,则会抛出异常

Exception in thread "main" java.lang.ClassCastException : test.Student cannot be cast to java.lang.Comparable
     at java.util.ComparableTimSort.countRunAndMakeAscending( ComparableTimSort.java:290)
     at java.util.ComparableTimSort.sort(ComparableTimSort.java:157 )
     at java.util.ComparableTimSort.sort(ComparableTimSort.java:146 )
     at java.util.Arrays.sort( Arrays.java:472 )
     at test.ComparableDemo1.main(ComparableDemo1.java:59 )



此异常是由于类型转换错误。。。因为在排序的时候,所有的对象都要向Comparable接口转换

上面的例子用到的排序方法是二叉树排序。


          二叉树实现的基本原理是:将第一个内容作为根节点保存,如果后面的值比根节点小,则放在根节点的左子树,如果后面的值比根节点大,则放在根节点的右子树。

如果给出:8,3,10, 9, 1,5



然后根据中序遍历的方式来读出(中序遍历就是左子树-->根节点--->右子树)

               1,3,5,8,9,10

下面的代码是来实现二叉树的代码:

class BinaryTree{
      class Node{ //声明一个节点类
               private   Comparable data; //保存具体的内容
              
               private Node left ;//保存 左节点
              
               private Node  right ;//保存右节点
              
               private void addNode(Node newNode){
                    if (newNode. data.compareTo( this .data )<0){ //放在左子数
                         if (this .left == null){
                              this .left =newNode;
                        } else {
                              this .left .addNode(newNode);
                        }
                        
                   } if (newNode. data.compareTo( this .data )>=0){ //放在右子树
                         if (this .right == null){
                              this .right =newNode;
                        } else {
                              this .right .addNode(newNode);
                        }
                   }
                   
              }
              
               public void printNode(){
                    if (this .left != null){
                         this .left .printNode();
                   }
                   System. out .print(this .data +"\t" );//输出根节点
                    if (this .right != null){
                         this .right .printNode();
                   }
              }
              
              
          
     }
      private Node root; //根元素
     
      public void add(Comparable data){
          Node newNode= new Node();//每加入一个内容,就声明一个节点
          newNode. data=data;
           if (root == null){
               root=newNode;
          } else {
               root.addNode(newNode);
          }
     }
     
      public void print(){
           this .root .printNode(); //输出节点
     }
}


public class ComparableDemo2 {
           public static void main(String[] args) {
               BinaryTree bt= new BinaryTree ();
              bt.add(8);
              bt.add(3);
              bt.add(10);
              bt.add(9);
              bt.add(1);
              bt.add(5);
              bt.add(3);
              bt.add(5);
              bt.add(0);
              bt.print();
          }

}


结果;
0    1    3    3    5    5    8    9    10   


3.如果项目前期没有实现Comparable接口,后期也还有一种接口Comparator接口

这个接口也有一种compareTo接口,只不过这个接口需要输入两个对象,实现方法与上面的一样,有兴趣的可以去实现下











原创粉丝点击