All implementations of collections share the same API, enabling the user to work with collection contents: add new elements, check the collection size, remove some elements from the collection, etc. The table below lists most frequently needed functions along with their descriptions. If you need to perform some more specific operation, please refer to the complete documentation on Java collections available at https://docs.oracle.com/javase/1.5.0/docs/guide/collections/.
Function | Description |
---|---|
int size() | Returns the number of elements in the list. |
boolean isEmpty() | Tests if this list has no elements. Returns true if the container has no elements, and false otherwise. |
boolean add(<ElementClass> element) | Appends the specified element to the end of this list. Ensures that the container holds the argument. Returns false if the adding was not performed. |
void add(int index, <ElementClass> element) |
Inserts the specified element at the specified position in the list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). index — index at which the specified element is to be inserted. element — element to be inserted. Throws: IndexOutOfBoundsException — if the specified index is out of range (index < 0 || index > size()). |
boolean addAll(Collection c) |
Appends all of the elements in the specified Collection to the end of this list, in the order that they are returned by the specified Collection's Iterator. c — the elements to be inserted into this list. Returns: true if this list changed as a result of the call. |
void clear() | Removes all the elements from the collection. |
boolean contains(<ElementClass> element) | Returns true if this list contains the specified element. |
<ElementClass> get(int index) | Returns the element at the specified position in this list. |
boolean remove(<ElementClass> element) | Removes a single instance of the specified element from this list, if it is present. Returns true if the list contained the specified element. |
<ElementClass> set(int index, <ElementClass> element) | Replaces the element at the specified position in this list with the specified element. |
Object[] toArray() | Returns an array containing all the elements in the container. |
Object[] toArray(Object[] a) | Returns an array containing all the elements in the container, whose type is that of the array rather than plain Object (you must cast the array to the right type). |
There is no get() function for random-access element.
-
How can we improve this article?
-