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 }