[转载] java中对数组进行排序_如何在Java中对数组排序

参考链接: Java中main()的有效变体

java中对数组进行排序

 

  

  

 

 Java Array is like a container that can hold a fixed number of the same type of items, it can be primitive types as well as Objects.

  Java Array就像一个容器,可以容纳固定数量的相同类型的项目,它可以是原始类型也可以是对象。  

  Java中的数组排序 (Array Sorting in Java) 

 Sometimes we need to sort array in java, we can use Arrays class to sort the array. Arrays is a utility class that provides a lot of useful methods to work with arrays in java.

  有时我们需要在java中对数组进行排序 ,我们可以使用Arrays类对数组进行排序。 数组是一个实用程序类,提供了许多有用的方法来使用Java中的数组。  

 

  

 Let’s look at an example to sort an array in Java.

  让我们看一个在Java中对数组排序的示例。  

 package com.journaldev.sort;

 

import java.util.Arrays;

 

public class JavaArraySort {

 

    /**

     * This class shows how to sort an array in Java

     * @param args

     */

    public static void main(String[] args) {

        int[] intArr = {1, 4, 2, 6, 3};

        String[] strArr = {"E", "A", "U","O","I"};

        //sort int array

        Arrays.sort(intArr);

        Arrays.sort(strArr);

        

        System.out.println(Arrays.toString(intArr));

        System.out.println(Arrays.toString(strArr));

    }

 

}

 Output of the above program is:

  上面程序的输出是:  

 

  

  

 

 [1, 2, 3, 4, 6]

[A, E, I, O, U]

 

  

  Array Sorting In Java

   Java中的数组排序  

 

  重要事项 (Important Points) 

 We can use Arrays.sort(T[] tArr) only if the array type implements Comparable interface. 仅当数组类型实现Comparable接口时,才能使用Arrays.sort(T[] tArr) 。 There is another variant of Arrays.sort(T[] tArr, Comparator c) that we can use to sort custom object array based on different fields. Arrays.sort(T[] tArr, Comparator c)还有另一个变体,我们可以使用它根据不同的字段对自定义对象数组进行排序。 You can head over to java comparable and comparator to learn about sorting an array using Comparator. 您可以转到Java可比较器和比较器,以了解使用Comparator对数组进行排序的知识。 Arrays.sort(T[] t) uses Dual-Pivot Quicksort algorithm with performance of O(n log(n)). The sorting is done in natural ascending order. Arrays.sort(T[] t)使用Dual-Pivot Quicksort算法 ,性能为O(n log(n)) 。 排序以自然升序进行。 

  用Java对数组进行排序有哪些不同的方法? (What are different ways to sort an array in Java?) 

 There are many algorithms to sort an array. Some of the popular ones are:

  有很多算法可以对数组进行排序。 一些受欢迎的是:  

 

  

  

 

 Bubble Sort 气泡排序 Insertion Sort 插入排序 Heap Sort 堆排序 Merge Sort 合并排序 Quick Sort 快速排序 

 We can write code to implement any of these algorithms to sort an array. However, it’s always recommended to use built-in Arrays.sort() function for error-free sorting and fast performance.

  我们可以编写代码来实现所有这些算法来对数组进行排序。 但是,始终建议使用内置的Arrays.sort()函数以实现无错误的排序和快速的性能。  

 

  翻译自: https://www.journaldev.com/784/sort-array-java

 

 java中对数组进行排序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值