您的位置:首页 > 其它

ArrayList之比较是否定义初始值的时间复杂度(2)

2016-08-01 16:27 225 查看
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));


运行结果:


定义初始容量大小运行时间较长
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐