https://blog.csdn.net/whp1473/article/details/79678974

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner; public class Main1 { public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
List<Integer> list = new ArrayList();
list.add(2);
list.add(-3);
list.add(90);
list.add(-4); //默认排序是从小到大
Collections.sort(list);
System.out.println(list.toString()); //重写compare方法,实现从大到小排序
Collections.sort(list, new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
return b - a;
}
});
System.out.println(list.toString());
}
}

  

问题描述
  给定一个长度为n的数列,将这个数列按从小到大的顺序排列。1<=n<=200
输入格式
  第一行为一个整数n。
  第二行包含n个整数,为待排序的数,每个整数的绝对值小于10000。
输出格式
  输出一行,按从小到大的顺序输出排序后的数列。
样例输入
5
8 3 6 4 9
样例输出
3 4 6 8 9
 
import java.util.Arrays;
import java.util.Scanner; public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int[] a = new int[n]; for(int i = 0; i < n; i++) {
a[i] = cin.nextInt();
}
Arrays.sort(a);
for(int i = 0; i < n; i++)
System.out.print(a[i] + " "); } }

  

5-java 排序, sort, collections.sort()的更多相关文章

  1. Java中使用Collections.sort()方法对数字和字符串泛型的LIst进行排序

    在List的排序中常用的是Collections.sort()方法,可以对String类型和Integer类型泛型的List集合进行排序. 首先演示sort()方法对Integer类型泛型的List排 ...

  2. python 中的sort 和java中的Collections.sort()函数的使用

    x=[1,2,3] x.sort()对的,x这个都变了 y=x.sort()错误 y=sorted(x)对的,x拍好序的一个副本 python中用匿名函数和自定义函数排序:(很奇怪的是比较函数返回的是 ...

  3. Java—集合框架 Collections.sort()、Comparable接口和Comparator接口

    Collentions工具类--java.util.Collections Collentions是Java集合框架中,用来操作集合对象的工具类,也是Java集合框架的成员,与List.Map和Set ...

  4. Java语言利用Collections.sort对Map,List排序

    1.main方法包含TreeMap排序1,TreeMap排序2,HashMap排序,List<Integer>排序,List<Bean>排序,List<Map>排序 ...

  5. 在Java中使用Collections.sort 依据多个字段排序

    一.如何使用Collections工具类进行排序 使用Collections工具类进行排序主要有两种方式: 1.对象实现Comparable接口,重写compareTo方法 /** * @author ...

  6. 6种字符串数组的java排序 (String array sort)

    注意,本文不是字符串排序,是字符串数组的排序. 方法分别是: 1.低位优先键索引排序 2.高位优先建索引排序 3.Java自带排序(经过调优的归并排序) 4.冒泡排序 5.快速排序 6.三向快速排序 ...

  7. java中Collections.sort()方法实现集合排序

    1.Integer/String泛型的List进行排序 List <Integer> integerlist = new ArrayList<Integer>();   //定 ...

  8. Collections.sort方法对list排序的两种方式

    Collections.sort( )分为两部分,一部分为排序规则,一部分为排序算法 . 规则用来判断对象,算法则考虑如何进行排序 对于自定义对象,sort()不知道规则,所以无法比较,这种情况下一定 ...

  9. Comparable和Comparator的区别&Collections.sort的两种用法

    在Java集合的学习中,我们明白了: 看到tree,可以按顺序进行排列,就要想到两个接口.Comparable(集合中元素实现这个接口,元素自身具备可比性),Comparator(比较器,传入容器构造 ...

随机推荐

  1. css:调整placeholder样式

    input::-webkit-input-placeholder, textarea::-webkit-input-placeholder { color: #C5CACF; } input:-moz ...

  2. zookeeper和dubbo的关系

    Dubbo建议使用Zookeeper作为服务的注册中心. 1.   Zookeeper的作用:         zookeeper用来注册服务和进行负载均衡,哪一个服务由哪一个机器来提供必需让调用者知 ...

  3. java为什么有些异常throw出去需要在函数头用throws声明,一些就不用。

    Excepiton分两类:checked exception.runtime exception:直接继承自Exception就是checked exception,继承自RuntimeExcepti ...

  4. webpack打包avalon+oniui+jquery

    随着avalon的发展壮大,我根据CSDN的统计数字,中国前端大概有1%的人在使用avalon了. avalon的最大优势是能兼容IE6,并且其API是非常稳定,只是在1.3.7 对ms-duplex ...

  5. [PHP]PHP的session机制,配置与高级应用

    ---------------------------------------------------------------------------------------------------- ...

  6. Working with the Dynamic Type in C#

    Working with the Dynamic Type in C# https://www.red-gate.com/simple-talk/dotnet/c-programming/workin ...

  7. 吴裕雄 oracle 函数、触发器和包编程

  8. 吴裕雄 20-MySQL NULL 值处理

    MySQL NULL 值处理我们已经知道 MySQL 使用 SQL SELECT 命令及 WHERE 子句来读取数据表中的数据,但是当提供的查询条件字段为 NULL 时,该命令可能就无法正常工作.为了 ...

  9. hdu3189-Just Do It-(埃氏筛+唯一分解定理)

    Just Do It Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  10. property 、classmethod 、 staticmethod 的用法

    @property # property是一个装饰器函数 ,作用:将一个方法伪装成属性 # 所有的装饰器函数都怎么用? 在函数.方法.类的上面一行直接@装饰器的名字 # 装饰器的分类: # 装饰函数 ...