Collections用法總結

2022-07-12 13:30:16 字數 960 閱讀 4503

collections是乙個包裝類,其中包含有各種有關集合操作的靜態多型方,比如可以作用在list和set上,此類不能例項化。

排序integer array = new integer;

listintegers = arrays.aslist(array);

collections.sort(integers);

out.println(integers); //[0, 2, 3, 4, 10]

在這裡借用了arrays的aslist方法,該方法可以將陣列轉成乙個list。這裡也只能對封裝型別integer陣列呼叫aslist方法,如果是int型的陣列呼叫aslist會出現其他問題。可以查考arrays用法總結。

混排out.println(integers); //[0, 2, 3, 4, 10]

collections.shuffle(integers);

out.println(integers); //[0, 3, 10, 2, 4]

反轉out.println(integers); //[0, 3, 10, 2, 4]

collections.reverse(integers);

out.println(integers); //[3, 0, 4, 2, 10]

填充(覆蓋)元素

array = new integer;

integers = arrays.aslist(array);

collections.fill(integers, -1);

out.println(integers); //[-1, -1, -1, -1, -1]

out.println(arrays.tostring(array)); //[-1, -1, -1, -1, -1]

通過輸出可以發現原陣列的值也跟著發生了變化,因為是封裝型別,所以integer中和list中的元素指向的是用乙個integer物件的位址,所以兩個會一起變化。

Collections用法總結

collections是乙個包裝類,其中包含有各種有關集合操作的靜態多型方,比如可以作用在list和set上,此類不能例項化。integer array new integer list integers arrays.aslist array collections.sort integers o...

Collections方法總結

collections類它沒有構造方法,而為了能夠操作它的方法呢,其中的方法就都被定義成了靜態的,這樣就可以用collections.方法名來操作它的方法了。一 sort 方法可以對list集合進行排序 按照的是所加物件的自然順序比較,若所填物件不具備比較性就要讓物件去實現comparable介面。...

Collections常用的方法總結

1 sort collection 方法的使用 含義 對集合進行排序 例 對已知集合c進行排序?public class practice 執行結果為 l,o,v,e e,l,o,v 2 shuffle collection 方法的使用 含義 對集合進行隨機排序 例 shuffle collecti...