---------------------- ASP.Net+Unity開發、.Net培訓、期待與您交流! ----------------------
一,Collections中的方法
對List集合排序:
static <T extends Comparable<? super T>>void sort(List<T> list);
static <T> void sort(List<T>list,Comparator<? super T>compar);
獲取集合中的最大值
static <T extends Comparable<? super T>> T max(Collection<? extends T> coll);
static <T> T max(Collection<? extends T> coll,Comparator<? super T> compar);
集合中的二分查找
static<T> int binarySearch(List<? extends Comparable<? super T>> list,T key);
static <T> int binarySearch(List<? extends Comparable<? super T>> list,T key, Comparator<? super T> comp)
替換和反轉
fill方法可以將list集合中所有元素替換成指定元素
替換:
Collections.fill(List<? super T> list, T obj)
Collections.replaceAll(List<T>list,T oldVal,T newVal);
反轉:
list集合中反轉元素
Collections.reverse(List<T> list)
反轉比較器
Comparator<T> Collections.reverseOrder();
Comparator<T> Collections.reverseOrder(Comparator<T> cmop)
返回線程同步的集合(其底層就是實現了List,Map,Set集合然後複寫它們的方法)
List<T> synchronizedList(List<T> list)
Set<T> synchronizedList(Set<T> set)
Map<K,V> synchronizedList(Map<K,V> map)
置換:
Static vid swap(List<T>list int i,int j)
隨機對集合進行排放:
static void shuffle(List<T> list);將list集合中元素進行隨機排放
static void shuffle(List<?> list, Random rnd)
二、 Arrays:用於操作數組的工具類
asList:將數組變成list集合
1,把數組變成list集合有什麼好處?
可以使用集合的思想去操作數組中的元素
注意:將數組變成集合時,不可以使用集合的增刪方法,因爲數組的長度是固定的。
如果你增刪了,那麼會發生:UnsupportedOperationException
如果數組中的元素都是對象 ,那麼變成集合時,數組中的元素就直接轉成集合中的元素存在,如果數組中的
元素都是基本數據類型,那麼 會將該數組作爲集合中的元素存在。
2,Collections中的方法將集合變數組
static Object [] toArray()
static <T> T[] toArray(T[] a)
3,static <T> T[] toArray(T[] a)中的數組的長度要定義爲多長呢?
當指定類型的數組長度小於了集合的size,那麼該方法內部會創建一個新的數組,長度爲集合的size.
當指定類型的數組長度大於了集合的size,就不會在創建新的數組了,而是使用傳遞進來的數組,所以
創建一個剛剛好的數組最好。
4爲什麼要將集合變數組?
爲了限定對元素的操作,不需要進行增刪了。
---------------------- ASP.Net+Unity開發、.Net培訓、期待與您交流! ----------------------