JAVA中sort函数的使用方法的个人总结
首先:sort函数的基本格式(默认排序为升序排序)
- Arrays.sort(数组名,起始下标,终止下标);
我们举个简单的例子
- import java.util.*;
- import java.util.Arrays;
- public class Main {
- public static void main(String[] args) {
- Scanner in=new Scanner(System.in);
- while(in.hasNext())
- {
- int num[]=new int[100];
- int n;///输出n个数
- n=in.nextInt();
- for(int i=0;i<n;i++)
- {
- num[i]=in.nextInt();
- }
- Arrays.sort(num,0,n);///排序部分
- for(int i=0;i<n;i++)
- {
- System.out.println(num[i]);
- }
- }
- }
- }
这个代码输入n个数,并把他们从小到大排序,运行结果如下。
大家注意一下,这里的起始下标和终止下标一定要是整形数。不能是浮点型。不然会报
用sort函数对浮点数呢?
- import java.util.*;
- import java.util.Arrays;
- public class Main {
- public static void main(String[] args) {
- Scanner in=new Scanner(System.in);
- while(in.hasNext())
- {
- double num[]=new double[100];
- int n;///输出n个数
- n=in.nextInt();
- for(int i=0;i<n;i++)
- {
- num[i]=in.nextDouble();
- }
- Arrays.sort(num,0,n);///排序部分
- for(int i=0;i<n;i++)
- {
- System.out.println(num[i]);
- }
- }
- }
- }
运行结果如下
如果一个数组初始化时已经赋值。则SORT函数可以另外一种格式
- Arrays.sort(数组名);
举个例子
- import java.util.*;
- import java.util.Arrays;
- public class Main {
- public static void main(String[] args) {
- Scanner in=new Scanner(System.in);
- int num[]= {5,4,3,2,1};
- Arrays.sort(num);
- for(int i=0;i<5;i++)
- {
- System.out.println(num[i]);
- }
- }
- }
然而,只是单纯的降序升序排序是满足不了使用需求得。
所以,我们要研究下sort函数中cmp函数的使用方法
我们来看看cmp函数的格式
- int compare(Object o1, Object o2);
我们可以看到,传入函数的是java中的类(java中没有结构体)
这时,sort函数的格式变为
- Arrays.sort(数组名, 起始下标, 终止下标, new cmp());
怎么自定义排序呢?
基本方法
int compare(Object o1, Object o2) 返回一个基本类型的整型
如果要按照升序排序,
则o1 小于o2,返回-1(负数),相等返回0,01大于02返回1(正数)
如果要按照降序排序
则o1 小于o2,返回1(正数),相等返回0,01大于02返回-1(负数)
我们来举个例子。-----输入n个数,然后降序排序。
- import java.util.Arrays;
- import java.util.Comparator;
- import java.util.Scanner;
- import java.util.*;
- class shu ///创建类
- {
- int x;
- }
- class cmp implements Comparator<shu> {
- /*
- * 因为上面指定了类型<he>,所以此处可以直接(he A,he B) 否则要写成(Object A,Object
- * B),再强制转换成he类型:((he)A).x
- */
- public int compare(shu A, shu B) ///降序排序
- {
- if(A.x<B.x)
- {
- return 1;
- }
- else if(A.x==B.x)
- {
- return 0;
- }
- else
- {
- return -1;
- }
- }
- }
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- while (in.hasNext()) {
- shu num[] = new shu[100];///创建类数组
- int n;
- n = in.nextInt();
- for (int i = 0; i < n; i++) {
- num[i]=new shu();///这个地方容易漏
- num[i].x = in.nextInt();
- }
- Arrays.sort(num, 0, n, new cmp());
- for (int i = 0; i < n; i++) {
- System.out.println(num[i].x);
- }
- }
- }
- }
运行结果
为了更好地理解,我们来举一些关于java结构体排序的题目
例一:HRBUST 1095 最麻烦了 点击打开链接
- import java.util.Arrays;
- import java.util.Comparator;
- import java.util.Scanner;
- import java.util.*;
- class shu ///创建类
- {
- int acm;
- int mon;
- int rp;
- }
- class cmp implements Comparator<shu> {
- public int compare(shu A, shu B) ///降序排序
- {
- if(A.acm==B.acm)
- {
- if(A.mon==B.mon)
- {
- if(A.rp<B.rp)
- {
- return 1;
- }
- else if(A.rp==B.rp)
- {
- return 0;
- }
- else
- {
- return -1;
- }
- }
- else
- {
- if(A.mon<B.mon)
- {
- return 1;
- }
- else if(A.mon==B.mon)
- {
- return 0;
- }
- else
- {
- return -1;
- }
- }
- }
- else
- {
- if(A.acm<B.acm)
- {
- return 1;
- }
- else if(A.acm==B.acm)
- {
- return 0;
- }
- else
- {
- return -1;
- }
- }
- }
- }
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- while (in.hasNext()) {
- shu num[] = new shu[1005];///创建类数组
- int n;
- n = in.nextInt();
- for (int i = 0; i < n; i++) {
- num[i]=new shu();///这个地方容易漏
- num[i].acm = in.nextInt();
- num[i].mon = in.nextInt();
- num[i].rp = in.nextInt();
- }
- Arrays.sort(num, 0, n, new cmp());
- for (int i = 0; i < n; i++) {
- System.out.println(num[i].acm+" "+num[i].mon+" "+num[i].rp);
- }
- }
- }
- }
- //int compare(Object o1, Object o2) 返回一个基本类型的整型
- //如果要按照升序排序,
- //则o1 小于o2,返回-1(负数),相等返回0,01大于02返回1(正数)
- //如果要按照降序排序
- // 则o1 小于o2,返回1(正数),相等返回0,01大于02返回-1(负数)
- //
- import java.util.*;
- class mans
- {
- long acm,money,rp;
- }
- class cmp implements Comparator<mans>
- {
- public int compare(mans a,mans b)
- {
- if(a.acm==b.acm)
- {
- if(a.money==b.money)
- {
- return (int)(a.rp-b.rp);
- }
- return (int)(a.money-b.money);
- }
- return (int)(a.acm-b.acm);
- }
- }
- public class Main
- {
- public static void main(String[] args)
- {
- // TODO Auto-generated method stub
- Scanner scanf=new Scanner(System.in);
- mans a[]=new mans[1009];
- while(scanf.hasNext())
- {
- int n=scanf.nextInt();
- for(int i=0;i<n;i++)
- {
- a[i] = new mans();
- a[i].acm=scanf.nextLong();
- a[i].money=scanf.nextLong();
- a[i].rp=scanf.nextLong();
- }
- Arrays.sort(a,0,n,new cmp());
- for(int i=n-1;i>=0;i--)
- {
- System.out.println(a[i].acm+" "+a[i].money+" "+a[i].rp);
- }
- }
- }
- }
但是更多的题目中,我们会遇到字典序排序的情况
这里,我们再举一个例子
例2:HRBUST 1023 JiaoZhu and C 点击打开链接
这个代码提交会超时!!!!!!!!!!!!!
- import java.util.Arrays;
- import java.util.Comparator;
- import java.util.Scanner;
- import java.util.*;
- class shu ///创建类
- {
- String name;///比较时用String
- int mon;
- int hunt;
- }
- class cmp implements Comparator<shu> {
- public int compare(shu A, shu B)
- {
- if(A.hunt==B.hunt)
- {
- if(A.mon==B.mon)
- {
- int flag=(A.name).compareTo(B.name);///按字典序排序
- if(flag==0)
- {
- return 0;
- }
- else if(flag<0)
- {
- return -1;
- }
- else
- {
- return 1;
- }
- }
- else
- {
- if(A.mon==B.mon)
- {
- return 0;
- }
- else if(A.mon<B.mon)
- {
- return -1;
- }
- else
- {
- return 1;
- }
- }
- }
- else
- {
- if(A.hunt==B.hunt)
- {
- return 0;
- }
- else if(A.hunt<B.hunt)
- {
- return 1;
- }
- else
- {
- return -1;
- }
- }
- }
- }
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- while (in.hasNext()) {
- shu num[] = new shu[100005];///创建类数组
- int n;
- n = in.nextInt();
- for (int i = 0; i < n; i++) {
- num[i]=new shu();///这个地方容易漏
- num[i].name=in.next();
- num[i].hunt=in.nextInt();
- num[i].mon=in.nextInt();
- }
- Arrays.sort(num, 0, n, new cmp());
- for (int i = 0; i < n; i++) {
- System.out.println(num[i].name);
- }
- }
- }
- }
相关推荐
- java 中重载函数调用时出现的问题
- C++标准库中的sort函数
- Java 1.8中的Metaspace总结
- GOF 的23种JAVA常用设计模式总结 02 UML中的类图与类图之间的关系
- c++中sort()函数的用法简介
- 总结了一下java基础!----以后慢慢更新,因为个人原因,这些都不在给我测试的代码,有想学的!请自己检验
-
关于
看后的一些总结-2 - java基础---Java中守护线程的总结
- Java中守护线程的总结
- java中的方法(函数)
- iTunes 安装终极解决方案
- 初始化vue项目,报错This is probably not a problem with npm,there is likely additional logging output above