使用Java的Comparable和Comparator介面排序三元組陣列
在本文中,我們將建立一個三元組陣列,並嘗試使用Comparable和Comparator介面對其進行排序。三元組陣列是指包含三個元素的陣列。
一個陣列是一種線性資料結構,用於儲存具有相似資料型別的元素組。它以順序方式儲存資料。一旦我們建立了一個數組,我們就無法改變其大小,即它是固定長度的。
使用Comparator排序三元組陣列
顧名思義,Comparator用於比較某些東西。在Java中,Comparator是一個介面,用於排序自定義物件。我們可以在其名為compare()的內建方法中編寫我們自己的邏輯來排序指定的物件。此方法接受兩個物件作為引數,然後返回一個整數值。透過這個整數值,Comparator決定哪個物件更大。
語法
Comparator< TypeOfComparator > nameOfComparator = new Comparator< TypeOfComparator >() { compare( type object1, type object1 ) { // logic for comparison } };
將用於排序操作的nameOfComparator傳遞給Arrays.sort()方法。
Arrays.sort()
它是Arrays類的靜態方法,它接受一個引數並相應地對其元素進行排序。此方法可以排序數值資料型別的陣列,例如整數或雙精度數,甚至字元陣列、字串陣列。
語法
Arrays.sort(nameOfarray);
示例
在這個例子中,建立一個比較器,並在其中覆蓋其compare()方法,該方法將包含排序邏輯。然後,建立一個三元組陣列,並使用內建方法Array.sort()對其進行排序。
import java.util.*; class Cart { String item; double price; int quant; Cart(String item, int price, int quant) { // Constructor this.item = item; this.price = price; this.quant = quant; } } public class AraySort { public static void main(String args[]) { // use of comparator interface Comparator<Cart> comp = new Comparator<Cart>() { // logic to sort public int compare(Cart i, Cart j) { if(i.quant > j.quant) { return 1; } else { return -1; } } }; // creating triplet of array Cart[] obj = new Cart[3]; obj[0] = new Cart("Rice", 59, 5); obj[1] = new Cart("Milk", 60, 2); obj[2] = new Cart("Bread", 45, 1); Arrays.sort(obj, comp); // to sort System.out.println("Elements of the newly sorted array: "); for(int i = 0; i < obj.length; i++) { System.out.println("Item: " + obj[i].item + ", " + "Price: " + obj[i].price + ", " + "Quantity: " + obj[i].quant); } } }
上述程式碼的輸出如下:
Elements of the newly sorted array: Item: Bread, Price: 45.0, Quantity: 1 Item: Milk, Price: 60.0, Quantity: 2 Item: Rice, Price: 59.0, Quantity: 5
使用Comparable排序三元組陣列
當我們想按其自然順序排序自定義物件時,Comparable介面非常有用。例如,它按字典順序排序字串,按數字順序排序數字。此介面位於java.lang包中。
語法
class nameOfclass implements Comparable<nameOfclass>
這裡,class是建立類的關鍵字,implements是啟用使用介面提供的功能的關鍵字。
compareTo()
Comparable介面只定義了一個名為CompareTo()的方法,可以重寫該方法以對物件的集合進行排序。它賦予了將類的物件與其自身進行比較的能力。當this物件等於傳遞的物件時,它返回0;如果this物件更大,則返回正值;否則返回負值。
語法
compareTo(nameOfclass nameOfobject);
示例
以下Java程式演示瞭如何使用Comparable介面對三元組陣列進行排序。在這裡,建立一個實現Comparable介面的類。在其compareTo()方法中定義排序邏輯。然後,在main方法中,建立一個三元組陣列,並使用Array.sort()方法對其進行排序。
import java.util.*; public class Cart implements Comparable<Cart>{ String item; double price; int quant; Cart(String item, int price, int quant) { // Constructor this.item = item; this.price = price; this.quant = quant; } // to compare public int compareTo(Cart comp) { if(this.quant > comp.quant) { return 1; } else { return -1; } } public static void main(String args[]) { // creating triplet of array Cart[] obj = new Cart[3]; obj[0] = new Cart("Rice", 59, 5); obj[1] = new Cart("Milk", 60, 2); obj[2] = new Cart("Bread", 45, 1); Arrays.sort(obj); // to sort System.out.println("Elements of the newly sorted array: "); for(int i = 0; i < obj.length; i++) { System.out.println("Item: " + obj[i].item + ", " + "Price: " + obj[i].price + ", " + "Quantity: " + obj[i].quant); } } }
執行後,它將顯示以下結果:
Elements of the newly sorted array: Item: Bread, Price: 45.0, Quantity: 1 Item: Milk, Price: 60.0, Quantity: 2 Item: Rice, Price: 59.0, Quantity: 5
結論
Comparable和Comparator介面都用於對給定列表或物件的元素進行排序,但是,Comparable介面修改原始類,而Comparator介面不修改原始類。在本文中,我們學習瞭如何使用這些介面對三元組陣列進行排序。