<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Java 中的`Comparable`接口 > 原文: [https://beginnersbook.com/2017/08/comparable-interface-in-java-with-example/](https://beginnersbook.com/2017/08/comparable-interface-in-java-with-example/) **`Comparable`接口**主要用于對**自定義對象**的數組(或列表)進行排序。 實現`Comparable`接口的對象列表(和數組)可以由`Collections.sort`(和`Arrays.sort`)自動排序。在我們看到如何對自定義對象的對象進行排序之前,讓我們看看如何對已經實現 `Comparable`的數組元素和包裝器類進行排序。 ## 示例:對數組和包裝器類進行排序 ```java import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo { public static void main(String[] args) { /* * Integer class implements Comparable * Interface so we can use the sort method */ int[] arr = {11,55,22,0,89}; Arrays.sort(arr); System.out.print("Sorted Int Array: "); System.out.println(Arrays.toString(arr)); /* * String class implements Comparable * Interface so we can use the sort method */ System.out.print("Sorted String Array: "); String[] names = {"Steve", "Ajeet", "Kyle"}; Arrays.sort(names); System.out.println(Arrays.toString(names)); /* * String class implements Comparable * Interface so we can use the sort method */ System.out.print("Sorted List: "); List fruits = new ArrayList(); fruits.add("Orange"); fruits.add("Banana"); fruits.add("Apple"); fruits.add("Guava"); fruits.add("Grapes"); Collections.sort(fruits); for(String s: fruits) System.out.print(s+", "); } } ``` **輸出:** ```java Sorted Int Array: [0, 11, 22, 55, 89] Sorted String Array: [Ajeet, Kyle, Steve] Sorted List: Apple, Banana, Grapes, Guava, Orange, ``` 在上面的示例中,您已經看到對實現`Comparable`接口的數組和對象列表進行排序是多么容易,您只需要調用`Collections.sort`(和`Arrays.sort`)。 **但是,如果要對自定義類的對象進行排序,則需要在自定義類中實現`Comparable`接口。** 該接口只有一個方法: ```java public abstract int compareTo(T obj) ``` 由于此方法是抽象的,因此如果實現`Comparable`接口,則必須在類中實現此方法。 讓我們舉個例子來更好地理解這個: ## 示例:通過實現`Comparable`接口對自定義對象進行排序 正如您所看到的,我在`Author`類中實現了`Comparable`接口,因為我想對這個類的對象進行排序。我已經在`compareTo()`方法中編寫了排序邏輯,您可以根據需求編寫邏輯。我想先按姓氏排序作者姓名,如果姓氏相同,則按名字排序。如果您只想按姓氏排序,那么`compareTo()`方法中的第一行就足夠了。 **`Author`類** ```java public class Author implements Comparable<Author> { String firstName; String lastName; String bookName; Author(String first, String last, String book){ this.firstName = first; this.lastName = last; this.bookName = book; } @Override /* * This is where we write the logic to sort. This method sort * automatically by the first name in case that the last name is * the same. */ public int compareTo(Author au){ /* * Sorting by last name. compareTo should return < 0 if this(keyword) * is supposed to be less than au, > 0 if this is supposed to be * greater than object au and 0 if they are supposed to be equal. */ int last = this.lastName.compareTo(au.lastName); //Sorting by first name if last name is same d return last == 0 ? this.firstName.compareTo(au.firstName) : last; } } ``` **排序類:`SortAuthByNames`** ```java import java.util.ArrayList; import java.util.Collections; public class SortAuthByNames{ public static void main(String args[]){ // List of objects of Author class ArrayList<Author> al=new ArrayList<Author>(); al.add(new Author("Henry","Miller", "Tropic of Cancer")); al.add(new Author("Nalo","Hopkinson", "Brown Girl in the Ring")); al.add(new Author("Frank","Miller", "300")); al.add(new Author("Deborah","Hopkinson", "Sky Boys")); al.add(new Author("George R. R.","Martin", "Song of Ice and Fire")); /* * Sorting the list using Collections.sort() method, we * can use this method because we have implemented the * Comparable interface in our user defined class Author */ Collections.sort(al); for(Author str:al){ System.out.println(str.firstName+" "+ str.lastName+" "+"Book: "+str.bookName); } } } ``` **輸出:** ```java Deborah Hopkinson Book: Sky Boys Nalo Hopkinson Book: Brown Girl in the Ring George R. R. Martin Book: A Song of Ice and Fire Frank Miller Book: 300 Henry Miller Book: Tropic of Cancer ``` > 注意:我們應該以這樣的方式編寫`compareTo()`方法:如果這個(我在這里指的是`this`關鍵字)小于傳遞的對象那么它應該返回負數,如果大于正數則為零,如果相等則返回 0。 你可能想知道為什么我沒有寫那個邏輯?因為名字和姓氏是字符串,所以我調用了字符串類的[`compareTo()`方法](https://beginnersbook.com/2013/12/java-string-compareto-method-example/),它完全相同。 但是如果我們比較的東西是其他類型的東西,比如`int`那么你就可以編寫這樣的邏輯: 假設`Employee`類的對象是(`empId`,`empName`,`empAge`),我們想通過`empAge`對對象進行排序]。 ```java public int compareTo(Employee e){ if(this.empAge==e.empAge) return 0; else if(this.empAge>e.empAge) return 1; else return -1; } ``` **或** ```java public int compareTo(Employee e){ return this.empAge > e.empAge ? 1 : this.empAge < e.empAge ? -1 : 0; } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看