62,615
社区成员
发帖
与我相关
我的任务
分享
import java.util.ArrayList;
import java.util.List;
public class FinalTest
{
public static void main(String[] args)
{
FilledList<CoutInt> foo = new FilledList<CoutInt>(CoutInt.class);
System.out.println(foo.create(14));
}
}
class CoutInt
{
private static int a;
private final int b = a++;
@Override
public String toString()
{
return Integer.toString(b);
}
}
class FilledList<T>
{
private Class<T> classtype;
public FilledList(Class<T> type)
{
this.classtype = type;
}
public List<T> create(int Elements)
{
List<T> result = new ArrayList<T>();
try{
for(int i=0;i<Elements;i++)
{
result.add(classtype.newInstance());
}
}catch(Exception ex)
{
throw new RuntimeException();
}
return result;
}
}