java.util 類
Collections 工具類
此類完全由在 collection 上進行操作或返回 collection 的靜態方法組成。它包含在 collection 上操作的多態算法,即“包裝器”,包裝器返回由指定 collection 支持的新 collection,以及少數其他內容。
// public static <T> void sort(List<T> list)
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(100);
list.add(10);
list.add(99);
list.add(1000);
//根據元素的自然順序 對指定列表按升序進行排序。列表中的所有元素都必須實現 Comparable 接口。
// public static <T> void sort(List<T> list, Comparator<? super T> c)
Collections.sort(list);
System.out.println(list);//[-10, 10, 12, 99, 100, 1000]
// public static <T> int binarySearch(List<?> list,T key) //使用二分搜索法搜索指定列表,以獲得指定對象。在進行此調用之前,必須根據列表元素的自然順序對列表進行升序排序
System.out.println(Collections.binarySearch(list,99));//3
// public static <T> T max(Collection<?> coll)
Integer max = Collections.max(list);//找到list中的最大值
System.out.println(max);//1000
// public static void reverse(List<?> list)反轉指定列表中元素的順序。
Collections.reverse(list);
System.out.println(list);
// public static void shuffle(List<?> list)此實現向後遍歷列表,從最後一個元素一直到第二個元素
Collections.shuffle(list);//使用默認隨機源對指定列表進行置換,相當於洗牌的功能。
System.out.println(list);
java.util
類 Arrays
此類包含用來操作數組(比如排序和搜索)的各種方法。此類還包含一個允許將數組作爲列表來查看的靜態工廠。
除非特別註明,否則如果指定數組引用爲 null,則此類中的方法都會拋出 NullPointerException。
//定義一個數組
int[] arr = { 12,34};
// public static void sort(int[] a)對指定的 int 型數組按數字升序進行排序
Arrays.sort(arr);// 默認sort方法只能對元素進行升序
// public static String toString(int[] a)返回指定數組內容的字符串表示形式
String s = Arrays.toString(arr);
System.out.println(s);//[1, 12, 23, 34, 41, 45, 67]
// public static int binarySearch(int[] a,int key)使用二分搜索法來搜索指定的int型數組以獲得指定的值
// 默認的二分查找,只針對升序數組有效;
int bin = Arrays.binarySearch(arr,41);
System.out.println(bin);//4
Collections 工具類
此類完全由在 collection 上進行操作或返回 collection 的靜態方法組成。它包含在 collection 上操作的多態算法,即“包裝器”,包裝器返回由指定 collection 支持的新 collection,以及少數其他內容。
// public static <T> void sort(List<T> list)
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(100);
list.add(10);
list.add(99);
list.add(1000);
//根據元素的自然順序 對指定列表按升序進行排序。列表中的所有元素都必須實現 Comparable 接口。
// public static <T> void sort(List<T> list, Comparator<? super T> c)
Collections.sort(list);
System.out.println(list);//[-10, 10, 12, 99, 100, 1000]
// public static <T> int binarySearch(List<?> list,T key) //使用二分搜索法搜索指定列表,以獲得指定對象。在進行此調用之前,必須根據列表元素的自然順序對列表進行升序排序
System.out.println(Collections.binarySearch(list,99));//3
// public static <T> T max(Collection<?> coll)
Integer max = Collections.max(list);//找到list中的最大值
System.out.println(max);//1000
// public static void reverse(List<?> list)反轉指定列表中元素的順序。
Collections.reverse(list);
System.out.println(list);
// public static void shuffle(List<?> list)此實現向後遍歷列表,從最後一個元素一直到第二個元素
Collections.shuffle(list);//使用默認隨機源對指定列表進行置換,相當於洗牌的功能。
System.out.println(list);
java.util
類 Arrays
此類包含用來操作數組(比如排序和搜索)的各種方法。此類還包含一個允許將數組作爲列表來查看的靜態工廠。
除非特別註明,否則如果指定數組引用爲 null,則此類中的方法都會拋出 NullPointerException。
//定義一個數組
int[] arr = { 12,34};
// public static void sort(int[] a)對指定的 int 型數組按數字升序進行排序
Arrays.sort(arr);// 默認sort方法只能對元素進行升序
// public static String toString(int[] a)返回指定數組內容的字符串表示形式
String s = Arrays.toString(arr);
System.out.println(s);//[1, 12, 23, 34, 41, 45, 67]
// public static int binarySearch(int[] a,int key)使用二分搜索法來搜索指定的int型數組以獲得指定的值
// 默認的二分查找,只針對升序數組有效;
int bin = Arrays.binarySearch(arr,41);
System.out.println(bin);//4