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 arrayArrays.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)

  1. We can use Arrays.sort(T[] tArr) only if the array type implements Comparable interface.仅当数组类型实现Comparable接口时,才能使用Arrays.sort(T[] tArr)
  2. 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)还有另一个变体,我们可以使用它根据不同的字段对自定义对象数组进行排序。
  3. You can head over to java comparable and comparator to learn about sorting an array using Comparator.您可以转到Java可比较器和比较器,以了解使用Comparator对数组进行排序的知识。
  4. 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:

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

  1. Bubble Sort气泡排序
  2. Insertion Sort插入排序
  3. Heap Sort堆排序
  4. Merge Sort合并排序
  5. 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中对数组进行排序

java中对数组进行排序_如何在Java中对数组排序相关推荐

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

    参考链接: Java中main()的有效变体 java中对数组进行排序 Java Array is like a container that can hold a fixed number of t ...

  2. scala中打印数组和集合_如何在Scala中打印数组?

    scala中打印数组和集合 Scala中的数组 (Array in Scala) In Scala, Array is a data structure that is a collection of ...

  3. java怎么清空数组的内容_如何在Java中清空数组

    使用List.clear()方法清空数组. 示例import java.util.ArrayList; import java.util.List; public class Tester { pub ...

  4. python set 排序_python set 排序_如何在Python中使用sorted()和sort()

    点击"蓝字"关注我们 ?"Python基础知识" 大卫·丰达科夫斯基  著 18财税3班 李潇潇    译 日期:2019年5月6日 一. 使用sorted() ...

  5. java对列表数据排序_如何在Java中对列表进行排序

    java对列表数据排序 Sometimes we have to sort a list in Java before processing its elements. In this tutoria ...

  6. java中按字母排序_如何在Java中按字母顺序对字符串进行排序

    使用toCharArray()方法 该类的toCharArray()方法将字符串转换为字符数组并返回它. 按字母顺序对字符串值进行排序获取所需的字符串. 使用toCharArray()方法将给定的字符 ...

  7. mysql 自然排序_如何在mysql中实现自然排序

    背景 熟悉mysql的同学应该清楚,mysql在对字符串做order by排序时是按照字典序进行排序的,但是如果字符串中包含数字的话(我们称这种类型的字符串为alphanumeric),仅按照字典序的 ...

  8. mysql按域排序_如何在MySQL中对域名进行排序?

    要对域名进行排序,请使用ORDER BY SUBSTRING_INDEX().让我们首先创建一个表-mysql> create table DemoTable670(DomainName tex ...

  9. java中md5加密和解密_如何在java中实现md5加密和解密

    如何在java中实现md5加密和解密 关注:273  答案:1  mip版 解决时间 2021-01-19 20:37 提问者精神疯裂 2021-01-19 05:36 如何在java中实现md5加密 ...

最新文章

  1. 【tf.keras】tf.keras使用tensorflow中定义的optimizer
  2. python怎么去学_Python第一步的学习,前辈告诉你怎么去学
  3. 一张图看懂encodeURI、encodeURIComponent、decodeURI、decodeURIComponent的区别
  4. sql 账号查询一个表查询权限_一个查询语句引发的问题以及巨型表相关操作探索与思考...
  5. Java整数类的compareTo()方法和示例
  6. Diskpart命令安装系统小结
  7. Python 玩出花了!一文教你用 Python 制作吃豆人游戏! | 附代码
  8. OpenGL 自制API gluPerspective
  9. Slicer学习笔记(六十一)slicer下segmentation segment合并
  10. Python获取下周一日期
  11. 如何解决苹果电脑(Macbook Pro)没有声音?
  12. 即将迎来M2的 MacBook Pro升级款 M2 Pro版本MacBook Pro或将于2023年3月发布
  13. 最新全国手机号码归属地信息SQLite数据库2019年2月更新
  14. 120道java最常问面试题!
  15. css hat,论切图仔的自我修养
  16. 申请人和审批人重复,怎么处理?
  17. 联想K29笔记本主板图片
  18. html css布局作品,130个漂亮CSS布局站点参考_CSS/HTML
  19. 1.8-19:肿瘤检测
  20. [转]网络推广经典方法

热门文章

  1. 使用jdk的xjc命令由schema文件生成相应的实体类
  2. java泛型 之 入门(interface)
  3. asp.net 导出word文档
  4. 24楼,此处风景独好
  5. [转载] python仿真入门_python基础-入门
  6. TP收集一些可以用的资源
  7. 【HDOJ5542】The Battle of Chibi
  8. Django(6)-orm
  9. 【java】之常用四大线程池用法以及ThreadPoolExecutor详解
  10. 程序员提高编程能力万无一失的办法