Better practice to re-instantiate a List or invoke clear()

前端 未结 4 388
执念已碎
执念已碎 2020-12-03 00:53

Using Java (1.6) is it better to call the clear() method on a List or just re-instantiate the reference?

I have an ArrayList that is filled with an unknown number of

4条回答
  •  佛祖请我去吃肉
    2020-12-03 01:37

    I think if the Arraylist is to be too frequently flushed,like if it's run continuously in loop or something then better use clear if the flushing is not too frequent then you may create a new instance.Also since you say that elements may vary from 10 object to millions you can probably go for an in-between size for each new Arraylist your creating so that the arraylist can avoid resizing a lot of time.

提交回复
热议问题