• 集合中的排序问题


    昨晚做的一个习题  

    描述

    现在给你两堆数,每个堆中有n个数。你的任务是把这两个堆并成一个堆,并把合并后的堆中的元素按从小到大的顺序输出。例如当n=5时,第一个堆是{1,2,3,4,5},第二个堆是{5,6,7,8,9},那么你就应该输出1 2 3 4 5 6 7 8 9。(一堆中可能有重复的数)

    输入

    第一行输入一个整数T(1≤T≤100),表示有T组测试数据。

    每组数据先输入一个整数N(1≤N≤10),表示每个堆中元素的个数。然后输入N个整数A(0≤A<100),最后输入N个整数B(0≤B<100)。

    输出

    把合并后的堆中的元素按从小到大的顺序输出。

    样例输入

    2
    5
    1 2 3 4 5
    5 6 7 8 9
    6
    18 88 43 5 10 78 
    94 99 37 92 3 52 

    样例输出

    1 2 3 4 5 6 7 8 9 
    3 5 10 18 37 43 52 78 88 92 94 99 

    用集合的排序方法  Collections.sort(); 及冒泡排序都能完美解决
    将代码整理如下 :

    public class Test6 {

    public static void main(String[] args) {
    int[] ins1 ={1,2,3,4,5};
    int[] ins2 ={5,6,7,8,9};
    changeShuZu(ins1,ins2);
    System.out.println("============");
    int[] ins3 ={18,88,43,5,10,78};
    int[] ins4 ={94,99,37,92,3,52};
    changeShuZu(ins3,ins4);
    }
    public static void changeShuZu(int[] ins1,int[] ins2){
    // int[] arr = new int[length];
    ArrayList<Integer> arr = new ArrayList<Integer>();

    for(int i=0;i<ins1.length;i++){
    arr.add(ins1[i]);
    }
    for(int i=0;i<ins2.length;i++){
    arr.add(ins2[i]);
    }
    for(int i=0;i<ins1.length;i++){
    for(int j=0;j<ins2.length;j++){
    if(ins1[i]==ins2[j]){
    arr.remove(i);
    }
    }
    }

    for (int i = 0; i < arr.size() - 1; i++) {
    for (int j = 1; j < arr.size() - i; j++) {
    Integer a;
    if ((arr.get(j - 1)).compareTo(arr.get(j)) > 0) { // 比较两个整数的大小

    a = arr.get(j - 1);
    arr.set((j - 1), arr.get(j));
    arr.set(j, a);
    }
    }
    }


    Collections.sort(arr); //
    for(int i=0;i<arr.size();i++){
    System.out.print(arr.get(i)+" ");
    }
    }
    }

    另 整理下 

    对List集合中的元素进行排序

    Collections对List集合中的数据进行排序

    有时候需要对集合中的元素按照一定的规则进行排序,这就需要用到

    Java中提供的对集合进行操作的工具类Collections,其中的sort方法

    先看一个简单的例子:

    [java] view plain copy
     
    1. public static void main(String[] args) {  
    2.     List<Integer> nums = new ArrayList<Integer>();  
    3.         nums.add(3);  
    4.         nums.add(5);  
    5.         nums.add(1);  
    6.         nums.add(0);  
    7.         System.out.println(nums);  
    8.         Collections.sort(nums);  
    9.         System.out.println(nums);  
    10. }  

    输出结果:
    [3, 5, 1, 0]
    [0, 1, 3, 5]

    稍微复杂的List里面放一个复杂的对象

    [java] view plain copy
     
    1. package core.java.collection.collections;  
    2.   
    3. public class User implements Comparable<User>{  
    4.       
    5.     private int score;  
    6.       
    7.     private int age;  
    8.       
    9.     public User(int score, int age){  
    10.         super();  
    11.         this.score = score;  
    12.         this.age = age;  
    13.     }  
    14.   
    15.     public int getScore() {  
    16.         return score;  
    17.     }  
    18.   
    19.     public void setScore(int score) {  
    20.         this.score = score;  
    21.     }  
    22.   
    23.     public int getAge() {  
    24.         return age;  
    25.     }  
    26.   
    27.     public void setAge(int age) {  
    28.         this.age = age;  
    29.     }  
    30.   
    31.     @Override  
    32.     public int compareTo(User o) {  
    33.         int i = this.getAge() - o.getAge();//先按照年龄排序  
    34.         if(i == 0){  
    35.             return this.score - o.getScore();//如果年龄相等了再用分数进行排序  
    36.         }  
    37.         return i;  
    38.     }  
    39.       
    40. }  
    41.   
    42. public static void main(String[] args) {  
    43.         List<User> users = new ArrayList<User>();  
    44.         users.add(new User(78, 26));  
    45.         users.add(new User(67, 23));  
    46.         users.add(new User(34, 56));  
    47.         users.add(new User(55, 23));  
    48.         Collections.sort(users);  
    49.         for(User user : users){  
    50.             System.out.println(user.getScore() + "," + user.getAge());  
    51.         }  
    52. }  

    输出结果:
    55,23
    67,23
    78,26
    34,56
    我们会发现sort(List<T>)方法中List中的T必须实现Comparable<T>接口,然后实现
    compareTo()方法,该方法的返回值0代表相等,1表示大于,-1表示小于;为什么
    在简单例子中没有看到实现Comparable接口呢?是因为Integer类其实自己已经实现
    了Comparable接口,Java已经给我们做好了。

    Collections提供的第二种排序方法sort(List<T> list, Comparator<? super T> c)
    先看例子:

    [java] view plain copy
     
    1. package core.java.collection.collections;  
    2.   
    3. public class Students {  
    4.       
    5.     private int age;  
    6.     private int score;  
    7.       
    8.     public Students(int age, int score){  
    9.         super();  
    10.         this.age = age;  
    11.         this.score = score;  
    12.     }  
    13.       
    14.     public int getAge() {  
    15.         return age;  
    16.     }  
    17.     public void setAge(int age) {  
    18.         this.age = age;  
    19.     }  
    20.     public int getScore() {  
    21.         return score;  
    22.     }  
    23.     public void setScore(int score) {  
    24.         this.score = score;  
    25.     }  
    26. }  
    27. public static void main(String[] args) {  
    28.         List<Students> students = new ArrayList<Students>();  
    29.         students.add(new Students(23, 100));  
    30.         students.add(new Students(27, 98));  
    31.         students.add(new Students(29, 99));  
    32.         students.add(new Students(29, 98));  
    33.         students.add(new Students(22, 89));  
    34.         Collections.sort(students, new Comparator<Students>() {  
    35.   
    36.             @Override  
    37.             public int compare(Students o1, Students o2) {  
    38.                 int i = o1.getScore() - o2.getScore();  
    39.                 if(i == 0){  
    40.                     return o1.getAge() - o2.getAge();  
    41.                 }  
    42.                 return i;  
    43.             }  
    44.         });  
    45.         for(Students stu : students){  
    46.             System.out.println("score:" + stu.getScore() + ":age" + stu.getAge());  
    47.         }  
    48. }  

    输出结果:
    score:89:age22
    score:98:age27
    score:98:age29
    score:99:age29
    score:100:age23

    从上面的例子我们可以看出Students类没有实现Comparable<T>接口,只是在sort()方法
    中多传入一个参数,只不过该参数是一个接口我们需要实现其compare方法。

    以上就是是Java中Colelctions工具类为我们提供的两种集合排序方法。

  • 相关阅读:
    mybatis中的#和$的区别
    spring ioc三种注入方式
    JSP中动态INCLUDE与静态INCLUDE的区别
    j2ee部分
    面试 框架部分
    注册Jdbc驱动程序的三种方式
    union和union all有什么不同?
    面试 JavaWeb 部分
    Cordova插件开发(iOS/Android)--看这篇就够了
    程序员,请不要只看技术
  • 原文地址:https://www.cnblogs.com/qianqian528/p/7985275.html
Copyright © 2020-2023  润新知