Java集合復習Collection
阿新 • • 發佈:2017-10-26
顯式 ext exc collect 尺寸 extend 實現 xtend 可變參數列表
1 import java.util.*; 2 class Snow{} 3 class Powder extends Snow{} 4 class Crusty extends Snow{} 5 class Slush extends Snow{} 6 7 class Light extends Powder {} 8 class Heavy extends Powder {} 9 10 11 public class AddingGroups { 12 13 public static void main(String[] args) { 14 15 Collection<Integer> collection = newArrayList<Integer>(Arrays.asList(1,2,3)); 16 Integer[] moreInts = {4,5,6}; 17 //Arrays.asList 顧名思義,傳進去一個數組,搞出來一個List。數組轉換為List 18 collection.addAll(Arrays.asList(moreInts)); 19 Collections.addAll(collection, 7);//可變參數列表 20 21 /* Arrays.asList()的底層實現是用數組,因此不能調整尺寸。所以運行時會出現UnsupportedOperationException22 List list = Arrays.asList(1,2,3); 23 list.add(4); 24 System.out.println(list); 25 */ 26 System.out.println(collection); 27 28 List<Snow> snow1 = Arrays.asList(new Powder(),new Slush(),new Crusty()); 29 30 List<Snow> snow2 = Arrays.asList(newLight(),new Heavy()); 31 32 // Collections.addAll(snow2); 33 34 List<Snow> snow3 = new ArrayList<Snow>(); 35 36 Collections.addAll(snow3, new Light(),new Heavy()); 37 38 //顯式類型參數說明 39 List<Snow> snow4 = Arrays.<Snow>asList(new Light(),new Heavy()); 40 41 42 } 43 44 }
Java集合復習Collection