标签:
package com.mufeng.theeleventhchapter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; public class AddingGroups { public static void main(String[] args) { Collection<Integer> collection = new ArrayList<Integer>(Arrays.asList( 1, 2, 3, 4, 5)); Integer[] moreInts = { 6, 7, 8, 9, 10 }; collection.addAll(Arrays.asList(moreInts)); Collections.addAll(collection, 11, 12, 13, 14, 15); Collections.addAll(collection, moreInts); for (Integer i : collection) { System.out.print(i + ", "); } System.out.println("\n-------------------我是分割线-------------------"); List<Integer> list = Arrays.asList(16, 17, 18, 19, 20); list.set(1, 99); for (Integer i : list) { System.out.print(i + ", "); } } }
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 6, 7, 8, 9, 10, -------------------我是分割线------------------- 16, 99, 18, 19, 20,
package com.mufeng.theeleventhchapter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; class Snow { } class Powder extends Snow { } class Light extends Powder { } class Heavy extends Powder { } class Crusty extends Snow { } class Slush extends Snow { } public class AsListInference { public static void main(String[] args) { List<Snow> snow1 = Arrays.asList(new Crusty(), new Slush(), new Powder()); // Type mismatch: cannot convert from List<Powder> to List<Snow> // List<Snow> snow2 = Arrays.asList(new Light(), new Heavy()); List<Snow> snow3 = new ArrayList<Snow>(); Collections.addAll(snow3, new Light(), new Heavy()); // Give a hint using an explicit type argument specification List<Snow> snow4 = Arrays.<Snow> asList(new Light(), new Heavy()); } }
标签:
原文地址:http://blog.csdn.net/u013693649/article/details/52136195