久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      總結(jié)分享Java比較兩個對象大小的三種方法

      本篇文章給大家?guī)砹岁P(guān)于java的相關(guān)知識,在優(yōu)先級隊列中插入的元素必須能比較大小,如果不能比較大小,如插入兩個學(xué)生類型的元素,會報ClassCastException異常。下面介紹了Java比較兩個對象大小的三種方法,希望對大家有幫助。

      總結(jié)分享Java比較兩個對象大小的三種方法

      寫爬蟲IP被封了怎么解決?立即使用

      推薦學(xué)習(xí):《java視頻教程》

      一. 為什么需要比較對象

      上一節(jié)介紹了優(yōu)先級隊列,在優(yōu)先級隊列中插入的元素必須能比較大小,如果不能比較大小,如插入兩個學(xué)生類型的元素,會報ClassCastException異常

      示例:

      class Student{     String name;     int age;       public Student(String name, int age) {         this.name = name;         this.age = age;     } } public class Test {     public static void main(String[] args) {         Student s1 = new Student("張三",25);         Student s2 = new Student("李四",31);         PriorityQueue<Student> p = new PriorityQueue<>();         p.offer(s1);         p.offer(s2);     } }

      結(jié)果:

      總結(jié)分享Java比較兩個對象大小的三種方法

      原因:因為優(yōu)先級隊列底層使用了堆數(shù)據(jù)結(jié)構(gòu),往堆中插入元素時,需要進行元素的比較,而Student是沒有辦法直接比較的,所以拋出異常

      二. 元素的比較

      1. 基本類型的比較

      Java中,基本類型的元素可以直接進行比較

      public class TestCompare {     public static void main(String[] args) {         int a = 10;         int b = 20;         System.out.println(a>b);         System.out.println(a==b);         System.out.println(a<b);           char c1 = 'a';         char c2 = 'b';         System.out.println(c1==c2);         System.out.println(c1>c2);         System.out.println(c1<c2);           boolean b1 = true;         boolean b2 = false;         System.out.println(b1==b2);         System.out.println(b1!=b2);     } }

      2. 引用類型的比較

      class Student{     String name;     int age;       public Student(String name, int age) {         this.name = name;         this.age = age;     } } public class Test {     public static void main(String[] args) {         Student s1 = new Student("張三",25);         Student s2 = new Student("李四",31);         Student s3 = s1;         System.out.println(s1==s2);  //false         System.out.println(s1==s3);  //true         //System.out.println(s1<s2); 編譯報錯         //System.out.println(s1>s3); 編譯報錯     } }

      從上述的結(jié)果來看,自定義類型不能使用>,<來比較,為什么可以使用==來比較?

      ==比較自定義類型時,比較的是對象的地址是否相同

      但是我們往往需要比較對象的內(nèi)容,如往優(yōu)先級隊列中插入某個對象,需要按照對象的內(nèi)容來調(diào)整堆,那如何比較呢?

      三. 對象比較的方法

      1. equals方法比較

      Object類是每一個類的基類,其提供了equals()方法來進行比較內(nèi)容是否相同

      總結(jié)分享Java比較兩個對象大小的三種方法

      但是Object中的equals方法默認(rèn)是用==來比較的,也就是比較兩個對象的地址 ,所以想讓自定義類型可以比較,可以重寫基類的equals()方法

      例:

      class Student{     String name;     int age;       public Student(String name, int age) {         this.name = name;         this.age = age;     }       @Override     public boolean equals(Object obj) {         if(this == obj){             return true;         }         if(obj==null || !(obj instanceof Student)){             return false;         }         Student s = (Student) obj;         return this.age==s.age && this.name.equals(s.name);     } } public class Test {     public static void main(String[] args) {         Student s1 = new Student("張三",25);         Student s2 = new Student("李四",31);         Student s3 = new Student("李四",31);         System.out.println(s1.equals(s2));         System.out.println(s2.equals(s3));     } }

      結(jié)果:可以比較內(nèi)容是否相同

      總結(jié)分享Java比較兩個對象大小的三種方法

      重寫equals方法的步驟

      • 如果兩個對象的地址相同,返回true
      • 如果傳入的對象為null,返回false
      • 如果傳入的對象與調(diào)用的對象不是同一個類型,返回false
      • 如果內(nèi)容都相同則返回true,否則返回false

      注意事項

      equals()方法只能比較兩個對象是否相同,不能按照>,<的方式來進行比較

      2. 基于Comparable接口的比較

      對于引用類型,如果想按照大小的方式進行比較,在定義類時實現(xiàn)Comparable接口,然后在類中重寫compareTo方法

      例:比較兩個人的大小,一般按照年齡來比較

      class Person implements Comparable<Person>{     String name;     int age;       public Person(String name, int age) {         this.name = name;         this.age = age;     }       @Override     public int compareTo(Person o) {         if(o == null){             return 1;         }         return this.age-o.age;     } } public class Test1 {     public static void main(String[] args) {         Person p1 = new Person("小王",22);         Person p2 = new Person("小張",21);         Person p3 = new Person("小方",22);         System.out.println(p1.compareTo(p2)); //>0表示大于         System.out.println(p2.compareTo(p3)); //<0表示小于         System.out.println(p1.compareTo(p3)); //==0表示相等     } }

      compareTo方法是java.lang中的接口類,可以直接使用

      使用Comparable接口使得Student類型的對象可以插入到優(yōu)先級隊列中

      import java.util.PriorityQueue;   class Student implements Comparable<Student> {     String name;     int age;       public Student(String name, int age) {         this.name = name;         this.age = age;     }       @Override     public int compareTo(Student o) {         if(o == null){             return -1;         }         return this.age-o.age;     } } public class Test {     public static void main(String[] args) {         Student s1 = new Student("張三",25);         Student s2 = new Student("李四",31);         Student s3 = new Student("李四",35);         PriorityQueue<Student> p = new PriorityQueue<>();         p.offer(s1);         p.offer(s2);         p.offer(s3);     } }

      結(jié)果:Student類型的對象也可以插入優(yōu)先級隊列中

      總結(jié)分享Java比較兩個對象大小的三種方法

      3. 基于Comparator接口的比較

      按照比較器的方式比較具體步驟如下:

      • 創(chuàng)建一個比較器類,實現(xiàn)Comparator接口
      • 重寫compare方法

      使用比較器使得Student類型的對象可以插入到優(yōu)先級隊列中

      import java.util.Comparator; import java.util.PriorityQueue;   class Student {     String name;     int age;       public Student(String name, int age) {         this.name = name;         this.age = age;     } } class StudentComparator implements Comparator<Student>{     @Override     public int compare(Student o1, Student o2) {         if(o1 == o2){             return 0;         }         if(o1 == null){             return -1;         }         if(o2 == null){             return 1;         }         return o1.age-o2.age;     } } public class Test {     public static void main(String[] args) {         Student s1 = new Student("張三",25);         Student s2 = new Student("李四",31);         Student s3 = new Student("李四",35);         PriorityQueue<Student> p = new PriorityQueue<>(new StudentComparator());         p.offer(s1);         p.offer(s2);         p.offer(s3);     } }

      結(jié)果:Student類型的對象可以插入到優(yōu)先級隊列中

      總結(jié)分享Java比較兩個對象大小的三種方法

      Comparator是java.util包中的泛型接口類,使用必須導(dǎo)入相應(yīng)的包

      4. 三種比較方式對比

      重寫的方法 說明
      Object.equals 只能比較兩個對象的內(nèi)容是否相等,不能比較大小
      Comparable.compareTo 類要實現(xiàn)接口,對類的侵入性較強,破壞了原來類的結(jié)構(gòu)
      Comparator.compare 需實現(xiàn)一個比較器類,對類的侵入性較弱,不破壞原來的類

      Comparable,Comparator使用哪種比較方式呢?

      如果拿到的是別人定義的類,我們不能對類進行操作,就選用創(chuàng)建類實現(xiàn)Comparator接口的方法

      如果類是用戶自己定義的類,可以對類進行操作,則采用實現(xiàn)Comparable接口的方法

      推薦學(xué)習(xí):《java視頻教程》

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號