list.clear()VS名单=新的ArrayList<整数GT;(); [英] list.clear() vs list = new ArrayList<Integer>();

查看:130
本文介绍了list.clear()VS名单=新的ArrayList<整数GT;();的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其中的2个选项一个是更好,更快地清除ArrayList和

Which one of the 2 options is better and faster to clear an ArrayList, and why?

list.clear() 

list = new ArrayList<Integer>();

它发生,我必须这样做,在随机时间,清除所有从我的ArrayList我条目,并没有办法知道有多少新的项目会出现在未来,有可能是0或1000哪种方法更快更好,为什么?

It happens that I have to, at random times, clear all entries from my ArrayList and I have no way to know how many new entries there will be in the future, there might be 0 or a 1000. Which method is faster and better, and why?

推荐答案

这是很难知道没有一个标杆,但如果你有很多的项目在ArrayList和平均规模较低,这可能会更快做出新ArrayList的。

It's hard to know without a benchmark, but if you have lots of items in your ArrayList and the average size is lower, it might be faster to make a new ArrayList.

<一个href=\"http://www.docjar.com/html/api/java/util/ArrayList.java.html\">http://www.docjar.com/html/api/java/util/ArrayList.java.html

public void clear() {
    modCount++;

    // Let gc do its work
    for (int i = 0; i < size; i++)
        elementData[i] = null;

    size = 0;
}

这篇关于list.clear()VS名单=新的ArrayList&LT;整数GT;();的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆