首頁 > Java > java教程 > 使用比較器將Java向量依降序排序

使用比較器將Java向量依降序排序

WBOY
發布: 2023-08-20 19:17:03
轉載
795 人瀏覽過

使用比較器將Java向量依降序排序

Vectors實作了List接口,用於建立動態數組。大小不固定且可以根據我們的需求成長的陣列稱為動態陣列。 Comparator是‘java.util’套件中可用的一個介面。

排序意味著按升序或降序重新排列給定列表或陣列的元素。在本文中,我們將建立一個向量,然後嘗試使用比較器按降序對其元素進行排序。

依降序排列Java向量的程式

Comparator

如其名稱所示,它用於比較某些東西。在Java中,Comparator是一個接口,用於對自訂物件進行排序。我們可以在其內建方法「compare()」中編寫自己的邏輯來對指定的物件進行排序。此方法接受兩個物件作為參數,然後傳回一個整數值。透過這個整數值,Comparator決定哪個物件更大。

Syntax

Comparator< TypeOfComparator > nameOfComparator = new Comparator< TypeOfComparator >() {
   compare( type object1, type object1 ) {
      // logic for comparison
   }
};
登入後複製

在像是‘Collection.sort()’這樣的方法中,nameOfComparator是用來排序作業的參數。

Collections.sort() method

The class ‘Collections’ of the Collection Interface provides a static method named ‘Collections.sort()’ that can sort elements of specified collections like ArrayList or LinkedList. It is available in ‘java.util’ package.##il’

Syntax

Collections.sort( nameOfcollection, ComparatorObject );
登入後複製

Collections.reverseOrder()

#It returns the comparator in reverse order.

Example 1

In the following example, we will define a vector named 'vectlist' and store a few objects in it by using the 'add()' method. Then, use the Comparator object and 'Collection.sort()' method to sort the vector in descending order.

import java.util.*;
public class VectClass {
   public static void main(String args[]) {
      // Creation of vector 
      Vector<Integer> vectList = new Vector<>();
      // Adding elements in the vector
      vectList.add(97);
      vectList.add(93);
      vectList.add(95);
      vectList.add(99);
      vectList.add(82);
      vectList.add(88);
      System.out.println("Elements of the unsorted list: ");
      // loop to iterate through elements
      for(int i = 0; i < vectList.size(); i++ ) {
         // to print the elements of the vector
         System.out.print(vectList.get(i) + " "); 
      }
      System.out.println();
      // Using comparator interface for sorting
      Comparator comp = Collections.reverseOrder();
      Collections.sort(vectList, comp);
      System.out.println("Elements of the newly sorted list: ");
      // loop to iterate through elements
      for(int i = 0; i < vectList.size(); i++ ) {
         // to print the elements of the new vector
		   System.out.print(vectList.get(i) + " "); 
      }
   }
}
登入後複製

輸出

Note: VectClass.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Elements of the unsorted list: 
97 93 95 99 82 88 
Elements of the newly sorted list: 
99 97 95 93 88 82 
登入後複製

Example 2

In this example, first, we will create a Comparator and inside it, we define our logic in 'compare()' method to sort the vector objects in descending order. The logic here states that take at objects in descending order。 and compare them using the if-else block. If first object is greater than second return -1 otherwise 1. Then, we pass the object of comparator to 'Collection.sort()' for sorting operation.

import java.util.*;
public class VectClass {
   public static void main(String args[]) {
      // Using comparator interface for sorting
      Comparator<Integer> comp = new Comparator<Integer>() {
         // logic to sort in descending order
         public int compare(Integer i, Integer j) {
            if(i < j) {
               return 1;
            } else {
               return -1;
            }
         }
      };
      // Creation of vector 
      Vector<Integer> vectList = new Vector<>();
      // Adding elements in the vector
      vectList.add(97);
      vectList.add(93);
      vectList.add(95);
      vectList.add(99);
      vectList.add(82);
      vectList.add(88);
      System.out.println("Elements of the unsorted list: ");
      // loop to iterate through elements
      for(int i = 0; i < vectList.size(); i++ ) {
         // to print the elements of the vector
		   System.out.print(vectList.get(i) + " "); 
      }
      System.out.println();
      Collections.sort(vectList, comp); // sort using comparator
      System.out.println("Elements of the newly sorted list: ");
      // loop to iterate through elements
      for(int i = 0; i < vectList.size(); i++ ) {
         // to print the elements of the new vector
         System.out.print(vectList.get(i) + " "); 
      }
   }
}
登入後複製

輸出

Elements of the unsorted list: 
97 93 95 99 82 88 
Elements of the newly sorted list: 
99 97 95 93 88 82 
登入後複製

結論

This article has explained the implementation of the Comparator Interface and also we discovered the use of a few inbuilt methods such as 'compareTo()', 'Collection.sort()' and 'Collections.reverseOrder()'.

以上是使用比較器將Java向量依降序排序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板