深入容器List

 

一种Generator解决方案:

所有Collection子类型都有一个接受另一个Collection对象的构造器,

用所接受的Collection对象中的元素来填充新的容器。为了更加容易

地创建测试数据,我们需要做的是构建接受Generator和quantity

数值并将他们作为构造器参数的类。

 

 

 

	public class CollectionData<T> extends ArrayList<T>{
		public CollecitonData(Generator<T> gen, int quantity){
			for(int i=0; i<quantity; i++)
				add(gen.next());p
		}

		public static <T> CollectionData<T> 
			list(Generator<T> gen, int quantity){
			return new CollectionData<T>(gen, quantity);
		}
	}

	class Government implemetns Generator<String>{
		Stirng[] foundation = ("Stringe women lying in ponds"+
			"distributing swords is no basis for a system of"+
			"government").split(" ");
		
		private int index;
		public String next(){
			return foundation[index++];
		}
	}

	public class CollectionDataTest{
		public static void main(String[] args){
			Set<String> set  =new LinkedHashSet<String>(
				new CollectionData<String>(new Government(),15));
			set.addAll(CollectionData.list(new Governmetn(), 15));
			System.out.println(set);
		}
	}
 

你可能感兴趣的:(list)