Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not clear on documentation of ArrayList

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?

like image 954
Cratylus Avatar asked Jan 17 '12 09:01

Cratylus


People also ask

How do you clear data from an ArrayList?

clear() method removes all of the elements from this list. The list will be empty after this call returns.

How do you clean an ArrayList in Java?

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.

How do you make a list clear in Java?

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.

How do you check if an ArrayList contains a value?

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.


1 Answers

It's a set operation, not an add. It just replaces the i-th entry of the array.

like image 111
Savino Sguera Avatar answered Oct 04 '22 01:10

Savino Sguera