5-java 排序, sort, collections.sort()
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个整数,为待排序的数,每个整数的绝对值小于10000。
8 3 6 4 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()的更多相关文章
- Java中使用Collections.sort()方法对数字和字符串泛型的LIst进行排序
在List的排序中常用的是Collections.sort()方法,可以对String类型和Integer类型泛型的List集合进行排序. 首先演示sort()方法对Integer类型泛型的List排 ...
- python 中的sort 和java中的Collections.sort()函数的使用
x=[1,2,3] x.sort()对的,x这个都变了 y=x.sort()错误 y=sorted(x)对的,x拍好序的一个副本 python中用匿名函数和自定义函数排序:(很奇怪的是比较函数返回的是 ...
- Java—集合框架 Collections.sort()、Comparable接口和Comparator接口
Collentions工具类--java.util.Collections Collentions是Java集合框架中,用来操作集合对象的工具类,也是Java集合框架的成员,与List.Map和Set ...
- Java语言利用Collections.sort对Map,List排序
1.main方法包含TreeMap排序1,TreeMap排序2,HashMap排序,List<Integer>排序,List<Bean>排序,List<Map>排序 ...
- 在Java中使用Collections.sort 依据多个字段排序
一.如何使用Collections工具类进行排序 使用Collections工具类进行排序主要有两种方式: 1.对象实现Comparable接口,重写compareTo方法 /** * @author ...
- 6种字符串数组的java排序 (String array sort)
注意,本文不是字符串排序,是字符串数组的排序. 方法分别是: 1.低位优先键索引排序 2.高位优先建索引排序 3.Java自带排序(经过调优的归并排序) 4.冒泡排序 5.快速排序 6.三向快速排序 ...
- java中Collections.sort()方法实现集合排序
1.Integer/String泛型的List进行排序 List <Integer> integerlist = new ArrayList<Integer>(); //定 ...
- Collections.sort方法对list排序的两种方式
Collections.sort( )分为两部分,一部分为排序规则,一部分为排序算法 . 规则用来判断对象,算法则考虑如何进行排序 对于自定义对象,sort()不知道规则,所以无法比较,这种情况下一定 ...
- Comparable和Comparator的区别&Collections.sort的两种用法
在Java集合的学习中,我们明白了: 看到tree,可以按顺序进行排列,就要想到两个接口.Comparable(集合中元素实现这个接口,元素自身具备可比性),Comparator(比较器,传入容器构造 ...
随机推荐
- 单件模式——Head First
一.定义 单件模式(Singleton Pattern)确保一个类只有一个实例,并提供一个全局访问点. 二.适用性 1.当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时. 2.当这个唯一实 ...
- linux command>file 2>&1 & 命令详解
command>file >& & 命令的最后一个&表示把该命令以后台的job的形式运行 一个命令的执行伴随着三种输入输出 标准输入(stdin):默认指向键盘的输 ...
- Haskell语言学习笔记(73)Existentials
Existentials(存在类型) Existentially quantified types(Existentially types,Existentials)是一种将一组类型归为一个类型的方式 ...
- 如何遍历Set对象
对 set 的遍历 1.迭代遍历: Set<String> set = new HashSet<String>(); Iterator<String> it = s ...
- mongoDB如何处理多对多关系
问题描述: 例如在关系数据库中有一个Team表,一个User表,两者是多对多的关系,即一个Team可以有多个User,一个User也可能属于多个Team,请问这样的关系在MongoDB中如何存储? 如 ...
- Ajax 学习 第三篇
1.什么是json 第一种方法 第二种方法 比较evar and jsondata 任何时候使用EVAR要特别小心,他不会管输入对象的类型 JSONLint可以在线校验代码的正确性 改写代码
- C# 模拟多文件上传
原地址:http://www.cnblogs.com/greenerycn/archive/2010/05/15/csharp_http_post.html 1.客户端代码 用winform写的 pr ...
- &符号 (弃用引用传参了,不要用!!)
写法一 $age = function grow($age) { $age += ; return $age; } echo grow($age) echo $age 写法二 $age = funct ...
- yii基础控制器安全验证
- struts2漏洞信息
渗透篇01-struts2漏洞利用 https://blog.csdn.net/qq_38055050/article/details/79841604 Struts2著名RCE漏洞引发的十年之思 ...