参考链接: 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中对数组进行排序

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

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

    java中对数组进行排序 Java Array is like a container that can hold a fixed number of the same type of items, ...

  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. MyBatis 通用Mapper 入门教程
  2. Prepare the Knowledge Base for Success
  3. HTML默认样式表CSS属性
  4. C# 10 新特性 —— CallerArgumentExpression
  5. RabbitMq 持久化(转)
  6. 线性表:4.结合顺序表和链表——静态链表及C语言实现
  7. sql的join语法解析
  8. 光电整纬机(日本世联电子株式会社)
  9. Springboot thymeleaf i18n国际化多语言选择
  10. [C++/CLI编程宝典][7]基本概念
  11. WORD出错:布局也会超出内容区
  12. SE-NET效果测试
  13. android 视频连续播放,VideoView实现视频无缝连续播放
  14. liunx 安装docker
  15. 东方影都2003_东方影都旅游攻略|电影博物馆amp;外景街最in玩法,get了吗?
  16. dd/MM/yyyy:HH:mm:ss +0800时间格式的转换
  17. VBA·编译错误:ByRef参数类型不符
  18. win系统如何运行msi后缀文件
  19. OneNote for windows10卸载及重装
  20. Network Slimming

热门文章

  1. NYOJ324 - 猴子吃桃问题
  2. 联想台式计算机排行榜,联想电脑品牌机排名介绍
  3. JavaScript创建对象的三种方式之利用字面量创建对象及使用方法(1)
  4. ie浏览器查看vue中js_浅析 Vue.js 中那些空间换时间的操作
  5. python 命令行运行 多进程_Python初学——多进程Multiprocessing
  6. LeetCode解题思路—滑动窗口法
  7. 在一个请求分页系统中,分别采用 FIFO、LRU和 OPT页面置换算法时,假如一个作业的页面走向为 4、3、2、1、4、3、5、4、3、2、1、5,当分配给该作业的物理块数M分别为 3、4时,
  8. Pikachu实验重现2(Sql的注入)
  9. 51nod-1358:浮波那契
  10. 51nod-1337:翻转游戏