17.1 完整的容器分类法
17.2 填充容器
package net.javabcsx.com.char17; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * Created by Administrator on 2016/4/14. */ class StringAddress{ private String s; public StringAddress(String s) { this.s = s; } @Override public String toString() { return super.toString() + " " + s; } } public class FillingLists { public static void main(String[] args) { List<StringAddress> list = new ArrayList<StringAddress>( //nCopies将对象拷贝4遍 Collections.nCopies(4, new StringAddress("Hello"))); System.out.println(list); //复制同一个对象替换容器 Collections.fill(list, new StringAddress("World")); System.out.println(list); } }
一种Generator解决方案