So, sort of in continuation of this post: What is the difference between ArrayList.clear() and ArrayList.removeAll()?... Are there certain situations where it is actually better to use removeAll()
instead of clear()
?
Also, to add onto this question, if I know I'm clearing all the contents of an ArrayList
, would it be ok to set it to a new ArrayList
?
ArrayList myList = new ArrayList<String>();
myList.add("a");
myList.add("b");
// instead of using: myList.clear();
myList = new ArrayList<String>()
If the above is ok to do, again, why use clear()
vs setting to a new ArrayList
? Creating a new, empty ArrayList
is faster than O(n).