ArrayList集合对象的sort()方法


public void sort(Comparator<? super E> c)
Parameters:c - the Comparator used to compare list elements. A null value indicates that the elements' natural ordering should be used

上面说明形参是实现Comparator接口的类对象,如果为null,则ArrayList集合的元素就按自然顺序来排序

代码验证如下:

 1 import java.util.*;
 2 
 3 public class Demo12{        
 4     public static void main(String[] args)throws Exception{  
 5         
 6         List<Integer> list = new ArrayList<>();
 7         list.add(94);
 8         list.add(45);
 9         list.add(28);
10         list.add(58);
11         
12         list.sort(null);
13         //遍历list集合元素
14         for(int i:list){
15             System.out.print(i + "\t");
16         }
17         
18         System.out.println();
19         
20         //转换为数组打印出来看看
21         System.out.println(Arrays.toString(list.toArray()));        
22     }
23 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM