当前位置: 首页 > article >正文

Arrays.sort()的用法

一、介绍

  • 1、sort(T[] a) 的使用
  • 2、sort(T[] a,int formIndex, int toIndex) 的使用
  • 3、sort(T[] a, Comparator<? supre T> c)的使用
  • 补充:sort(T[] a, Comparator<? supre T> c)类对象比较的使用
  • 补充:那么在参数中会出现super呢?这意味着这类型可以是T或者它的父类型。这就是的该方法可以允许所有子类使用相同的比较器。

Arrays.sort()的作用是对括号中的数组进行排序,时间复杂度O(n*logn),方法返回值为void。 是在原来数组的空间基础上进行升序排序,因此不需要定义一个数组接收它,即不需要返回值。
Arrays.sort()重载了四类方法:
sort(T[] a):对指定T型数组按数字升序排序。
sort(T[] a,int formIndex, int toIndex):对指定T型数组的指定范围按数字升序排序。
sort(T[] a, Comparator<? supre T> c): 根据指定比较器产生的顺序对T型数组进行排序。
sort(T[] a, int formIndex, int toIndex, Comparator<? supre T> c): 根据指定比较器产生的顺序对T型数组的指定范围进行排序。

1、sort(T[] a) 的使用

import java.util.Arrays;
import java.util.Comparator;public class ArraysSort {public static void main(String[] args) {int[] a={2,5,4,3,1,8};Arrays.sort(a);System.out.println(Arrays.toString(a));}
}// 结果// [1, 2, 3, 4, 5, 8]

2、sort(T[] a,int formIndex, int toIndex) 的使用

import java.util.Arrays;
import java.util.Comparator;public class ArraysSort {public static void main(String[] args) {int[] a={2,5,4,3,1,8};Arrays.sort(a,2,5);System.out.println(Arrays.toString(a));}
}// 结果
// [2, 5, 1, 3, 4, 8]

3、sort(T[] a, Comparator<? supre T> c)的使用

// 按第一维元素比较二维数组
import java.util.Arrays;
import java.util.Comparator;public class ArraysSort {public static void main(String[] args) {int[][] nums=new int[][]{{1,3},{1,2},{4,5},{3,7}};//方法一Arrays.sort(nums,new Comparator<int[]>(){@Overridepublic int compare(int[] a,int[] b){if(a[0]==b[0]){ // 不明白为什么要这样写  ,自己的基础有问题,不解return a[1]-b[1];}else{return a[0]-b[0];}}});for (int[] num : nums) {System.out.println(Arrays.toString(num));}}
}// 结果
/*
[1, 2]
[1, 3]
[3, 7]
[4, 5]
*/// 按照第二维元素比较二维数组import java.util.Arrays;
import java.util.Comparator;public class ArraysSort {public static void main(String[] args) {int[][] nums=new int[][]{{1,3},{1,2},{4,5},{3,7}};//方法一Arrays.sort(nums,new Comparator<int[]>(){@Overridepublic int compare(int[] a,int[] b){if(a[1]==b[1]){  //同样不解return a[0]-b[0];}else{return a[1]-b[1];}}});//方法二/*Arrays.sort(nums,(a,b)->a[1]-b[1]);*/for (int[] num : nums) {System.out.println(Arrays.toString(num));}}
}
// 结果
/*
[1, 2]
[1, 3]
[4, 5]
[3, 7]
*/

补充:sort(T[] a, Comparator<? supre T> c)类对象比较的使用

import java.util.Arrays;
import java.util.Comparator;class Dog{int size;int weight;public Dog(int s, int w){size = s;weight = w;}
}class DogSizeComparator implements Comparator<Dog>{@Overridepublic int compare(Dog o1, Dog o2) {return o1.size - o2.size;}
}class DogWeightComparator implements Comparator<Dog>{  // **在这里尖括号传入的是对象**@Overridepublic int compare(Dog o1, Dog o2) {return o1.weight - o2.weight;}
}public class ArraysSort {public static void main(String[] args) {Dog d1 = new Dog(2, 50);Dog d2 = new Dog(1, 30);Dog d3 = new Dog(3, 40);Dog[] dogArray = {d1, d2, d3};printDogs(dogArray);Arrays.sort(dogArray, new DogSizeComparator());printDogs(dogArray);Arrays.sort(dogArray, new DogWeightComparator());printDogs(dogArray);}public static void printDogs(Dog[] dogs){for(Dog d: dogs)System.out.print("size="+d.size + " weight=" + d.weight + " ");System.out.println();}
}// 结果
/*
size=2 weight=50 size=1 weight=30 size=3 weight=40 
size=1 weight=30 size=2 weight=50 size=3 weight=40 
size=1 weight=30 size=3 weight=40 size=2 weight=50 
*/

补充:那么在参数中会出现super呢?这意味着这类型可以是T或者它的父类型。这就是的该方法可以允许所有子类使用相同的比较器。

import java.util.Arrays;
import java.util.Comparator;class Animal{int size;
}class Dog extends Animal{public Dog(int s){size = s;}
}class Cat extends Animal{public Cat(int s){size  = s;}
}class AnimalSizeComparator implements Comparator<Animal>{@Overridepublic int compare(Animal o1, Animal o2) {return o1.size - o2.size;}
}public class ArraysSort {public static void main(String[] args) {Dog d1 = new Dog(2);Dog d2 = new Dog(1);Dog d3 = new Dog(3);Dog[] dogArray = {d1, d2, d3};printDogs(dogArray);Arrays.sort(dogArray, new AnimalSizeComparator());printDogs(dogArray);System.out.println();Cat c1 = new Cat(2);Cat c2 = new Cat(1);Cat c3 = new Cat(3);Cat[] catArray = {c1, c2, c3};printDogs(catArray);Arrays.sort(catArray, new AnimalSizeComparator());printDogs(catArray);}public static void printDogs(Animal[] animals){for(Animal a: animals)System.out.print("size="+a.size + " ");System.out.println();}
}// 结果
/*
size=2 size=1 size=3 
size=1 size=2 size=3 size=2 size=1 size=3 
size=1 size=2 size=3 
*/

巨人的肩膀:https://www.cnblogs.com/SupremeBoy/p/12717532.html

http://www.lryc.cn/news/2414599.html

相关文章:

  • 滚动条样式锦集
  • 2024年最新网络安全行业名词_失陷主机(1)
  • 前端入门之HTML与CSS
  • uaa认证服务流程
  • 认识headers
  • 揭秘Android Tombstone:崩溃位置的秘密研究-Crash Location
  • 使用ShellExecute函数实现以管理员身份运行程序
  • 常用配置文件-ini文件
  • JAVA静态变量是什么
  • 最短路径算法汇总
  • Java 快速入门指南
  • PLSQL Developer(安装、连接、汉化、注册图文教程)
  • 创建ROS消息(msg)和服务(srv)
  • 当你在浏览器输入www.xxx.com的时候会发生什么?
  • TLE两行轨道根数
  • QFAV——快速免费拼装你的视频会议
  • ubuntu 安装中文输入法(超简靠谱版)
  • 带宽是什么?
  • Java数字格式类 NumberFormat | DecimalFormat
  • rides介绍和安装
  • java web报表,jasperReport使用简介
  • git 某个分支代码回滚到某次push的步骤
  • 什么是线程安全和非线程安全
  • Java04方法
  • SFR解析算法 - SFR_Calculation (C语言)
  • Fiddler 4 安卓APP抓包教程
  • 施密特正交化(Gram-Schmidt Orthogonalization)
  • Python学习之pandas模块duplicated函数的常见用法
  • Oracle创建新用户以及配置权限(个人使用版)
  • 你在浏览器输入了baidu.com 并按下回车后,背后到底发生了什么?