Java 对实现了Comparable接口的数组和包装类进行排序

Java 对实现了Comparable接口的数组和包装类进行排序

Java提供了多种排序算法和方法,可以帮助我们对数组、列表或任何集合进行排序。当我们想按自然顺序对自定义对象进行排序时,可比较接口是一种有用的额外方式。例如,它按字典顺序排序字符串和按数值顺序排序数字。这个接口位于‘java.lang’包中。

在本文中,我们将创建一个数组和一个ArrayList,然后试图对它们进行排序,以展示数组和包装类已经实现了Comparable接口。

排序数组和包装类的元素

我们将使用以下方法来对集合和数组的元素进行排序-

Collections.sort()方法

Collection接口的类‘Collections’提供了一个名为‘Collections.sort()’的静态方法,可以对指定的集合(如ArrayList或LinkedList)的元素进行排序。它位于‘java.util’包中。

语法

Collections.sort( nameOfcollection );

Arrays.sort() 方法

这是一个由 Arrays 类提供的静态方法,它接受一个参数并对其元素进行排序。该方法可以对整数或浮点数等数值类型的数组进行排序,甚至可以对字符数组和字符串数组进行排序。

语法

Arrays.sort( nameOfarray);

步骤

  • 步骤1 - 我们将开始导入 ‘java.util’ 包,以便我们可以使用上面讨论的方法。

  • 步骤2 - 现在创建一个方法 ‘araySort’。在这个方法内部,声明并初始化一个名为 ‘aray’ 的整数类型数组。接下来,我们将使用内置的方法 ‘Arrays.sort()’ 对给定数组的元素进行升序排序。然后,使用 for each 循环打印新排序的数组。

  • 步骤3 - 创建另一个名为 ‘araylistSort()’ 的用户定义方法。在这个方法内部,我们定义一个包装类 Integer 的数组列表,并使用 ‘add()’ 方法向其中添加元素。现在使用内置的方法 ‘Collections.sort()’ 对给定数组列表的元素进行升序排序。然后,使用 for 循环内部的 ‘get()’ 方法打印排序后的数组列表的元素。

  • 步骤4 - 最后,在 main() 方法内部,我们将调用这两个方法来执行它们各自的操作。

示例

import java.util.*;
public class Comp1 {
   public static void araySort() {
      int aray[] = {9, 3, 56, 0, -2, -6, 2, 1, 80};
      System.out.print("The given unsorted array: ");
      // to print original unsorted array 
      for (int print : aray) {
         System.out.print(print + " ");
      } 
      Arrays.sort(aray);  
      // to sort the given array
      System.out.println();
      System.out.print("The newly sorted array: ");
      // to print newly sorted array
      for (int print : aray) {
         System.out.print(print + " ");
      }
      System.out.println();
   }
   public static void araylistSort() {
      // Creating arraylist of Wrapper class Integer
      ArrayList<Integer> araylist = new ArrayList<Integer>();
      // Adding elements in arraylist
      araylist.add(8);
      araylist.add(5);
      araylist.add(2);
      araylist.add(9);
      araylist.add(4);
      araylist.add(7);
      System.out.println("Elements of the list : ");
      // loop to iterate through elements
      for(int i = 0; i < araylist.size(); i++ ) {
         // to print the elements in the list
         System.out.print(araylist.get(i) + " "); 
      }
      Collections.sort(araylist); 
      // to sort the collection
      System.out.println();
      System.out.println("Elements of the newly sorted list : ");
      for(int i = 0; i < araylist.size(); i++ ) {
         // to print the elements of newly sorted list
         System.out.print(araylist.get(i) + " "); 
      }
   }
   public static void main(String args[]) {
      // method call
      araySort(); 
      araylistSort(); 
   }
}

输出

The given unsorted array: 9 3 56 0 -2 -6 2 1 80 
The newly sorted array: -6 -2 0 1 2 3 9 56 80 
Elements of the list : 
8 5 2 9 4 7 
Elements of the newly sorted list : 
2 4 5 7 8 9

我们创建的方法是静态的,所以我们不需要创建任何对象来调用它们。

结论

你一定注意到了我们没有显式地实现Comparable接口,但是我们可以使用’sort()’来对数组和包装类的元素进行排序。原因是它们隐式地实现了Comparable接口。

在本文中,我们了解了这两个内置的方法’Arrays.sort()’和’Collections.sort()’在排序方面的用处。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程