StudentVO student = null;
//保存当前时间的毫秒数
long begin1 = System.currentTimeMillis();
//创建集合时没有规定初始容量大小
Collection<StudentVO> list1 = new ArrayList<StudentVO>();
//循环一百万次,每一次向集合中添加一个对象
for (int i = 0; i < 1000000; i++) {
student = new StudentVO(i, "chenssy_" + i, i);
list1.add(student);
}
long end1 = System.currentTimeMillis();
System.out.println("list1 time:" + (end1 - begin1));
long begin2 = System.currentTimeMillis();
//创建对象时规定初始容量的大小
Collection<StudentVO> list2 = new ArrayList<StudentVO>(1000000);
for (int i = 0; i < 1000000; i++) {
student = new StudentVO(i, "chenssy_" + i, i);
list2.add(student);
}
long end2 = System.currentTimeMillis();
System.out.println("list2 time:" + (end2 - begin2));
运行结果:
定义初始容量大小运行时间较长