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 = new ArrayList<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()的底层实现是用数组,因此不能调整尺寸。所以运行时会出现UnsupportedOperationException 22 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(new Light(),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 }