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. angularjs 与 UEditor开发,添加directive,保证加载顺序正常

    'use strict'; angular.module('app.core').directive('ueditor', [function () { return { restrict: 'A', ...

  2. Hibernate 再接触 集合映射

    不太重要 List 用于排序 Map  key一般是user的某个字段(多半是主键 integer) package com.bjsxt.hibernate; import java.util.Has ...

  3. easyui分页,根据网友的一段代码优化了一下

    千言万语尽在代码中,可以自己看,不清楚留言吧! <%@ Page Language="C#" AutoEventWireup="true" CodeBeh ...

  4. 3G开发遇到的问题

    1.使用线程时,编译时要加上gcc xxx.c -o xxx -lpthread 2.分离字符串"abc,de,fgh" printf("%s",strtok ...

  5. pandas 常用清洗数据(二)

    1. df.head() Here we import pandas using the alias 'pd', then we read in our data. df.head - shows u ...

  6. 11.15java课后作业

    1,编写一个程序,指定一个文件夹,能自动计算出其总容量 package Account; import java.io.File; import java.util.ArrayList; public ...

  7. Nodejs this详解

    [Nodejs this详解] Nodejs中, 文件层this,指向的是module.export. 函数层this,指向的是global对象. 参考:http://www.jb51.net/art ...

  8. Controlled Components

    [Controlled Components] In HTML, form elements such as <input>, <textarea>, and <sele ...

  9. excel 数据量较大边查询边输入到excel表格中

    public Resultmodel getexpenseMessagx(HttpServletResponse response, String date1, String date2) { lon ...

  10. Shader基础(渲染管线)

    Shader原理部分 渲染绘图管线流程:     1.顶点处理(坐标系的转换) 一个模型有自身的方向(前方,上方,右方等)和中心点,既本地坐标系: 将这个模型放到场景当中,通过场景中的世界坐标原点和世 ...