Java ArrayList Methods
- add()
Adds an element to the list or inserts it at a specified index.public boolean add(E e) public void add(int index, E element)- addAll()
Adds all elements from a collection to the list, optionally at a given position.public boolean addAll(Collection extends E> c) public boolean addAll(int index, Collection extends E> c)- clear()
Removes all elements from the list, leaving it empty.public void clear()- clone()
Returns a shallow copy of the ArrayList.public Object clone()- contains()
Checks if the list contains a specific element.public boolean contains(Object o)- ensureCapacity()
Increases the capacity of the list to ensure it can hold more elements.public void ensureCapacity(int minCapacity)- forEach()
Applies an action to each element in the list.public void forEach(Consumer super E> action)- get()
Returns the element at the specified index.public E get(int index)- indexOf()
Returns the index of the first occurrence of the specified element.public int indexOf(Object o)- isEmpty()
Checks if the list has no elements.public boolean isEmpty()- iterator()
Returns an iterator to traverse the list.public Iteratoriterator() - lastIndexOf()
Returns the index of the last occurrence of the specified element.public int lastIndexOf(Object o)- listIterator()
Returns a list iterator for the list or from a specific index.public ListIteratorlistIterator() public ListIterator listIterator(int index) - remove()
Removes the element at the given index or removes the first occurrence of the specified element.public E remove(int index) public boolean remove(Object o)- removeAll()
Removes all elements that are also in the specified collection.public boolean removeAll(Collection> c)- removeIf()
Removes all elements that satisfy the given condition.public boolean removeIf(Predicate super E> filter)- removeRange()
Removes elements between the specified range of indexes.protected void removeRange(int fromIndex, int toIndex)- replaceAll()
Replaces each element using the specified operator.public void replaceAll(UnaryOperatoroperator) - retainAll()
Keeps only the elements that are also in the specified collection.public boolean retainAll(Collection> c)- set()
Replaces the element at the given index with a new one.public E set(int index, E element)- size()
Returns the number of elements in the list.public int size()- sort()
Sorts the list using the given comparator.public void sort(Comparator super E> c)- spliterator()
Creates a Spliterator for the list elements.public Spliteratorspliterator() - subList()
Returns a view of the portion of this list between fromIndex and toIndex.public ListsubList(int fromIndex, int toIndex) - toArray()
Returns an array containing all elements in this list.public Object[] toArray() publicT[] toArray(T[] a) - trimToSize()
Trims the capacity of the list to match its current size.public void trimToSize()
