The following part of the documentation for ArrayList does not seem correct to me:
The size, isEmpty, get, set, iterator, and listIterator operations run in constant time.
Now set is defined as:
set(int index, E element)
Replaces the element at the specified position in this list with the specified element.
So this could be used to add an element in the middle of the ArrayList and cause the rest of the elements to shift.
But this is considered linear operation and not constant.
Am I wrong here? Or am I missunderstaning something?
clear() method removes all of the elements from this list. The list will be empty after this call returns.
We can use ArrayList. clear() or ArrayList. removeAll() method to empty an ArrayList. The clear() method is the fastest as it only set the reference to the underlying array as null while the removeAll() will perform some additional work.
The clear() method of List interface in Java is used to remove all of the elements from the List container. This method does not deleted the List container, instead it justs removes all of the elements from the List.
ArrayList. contains() method can be used to check if a Java ArrayList contains a given item or not. This method has a single parameter i.e. the item whose presence in the ArrayList is tested. Also it returns true if the item is present in the ArrayList and false if the item is not present.
It's a set operation, not an add. It just replaces the i-th entry of the array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With