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