The ArrayList
class is part of the Java Collections Framework and provides resizable array functionality. It implements the List
interface and is widely used because of its flexibility in handling dynamic arrays.
This guide covers various methods available in the ArrayList
class, offering a comprehensive understanding of how to manipulate and interact with lists in Java. These methods are fundamental for efficient coding practices and help in performing operations like adding, removing, and accessing elements.
For more detailed information, you can refer to the official Java SE Documentation and additional resources on Java Collections Tutorial.
Method | Description |
---|---|
add() | Adds an element to the ArrayList. |
addAll() | Adds all elements from another collection to the ArrayList. |
addFirst() | Adds an element at the beginning of the ArrayList. |
addLast() | Adds an element at the end of the ArrayList. |
clear() | Removes all elements from the ArrayList. |
clone() | Creates a shallow copy of the ArrayList. |
contains() | Checks if the ArrayList contains a specified element. |
ensureCapacity() | Increases the capacity of the ArrayList. |
equals() | Compares the ArrayList with another collection for equality. |
forEach() | Performs an action for each element of the ArrayList. |
get() | Returns the element at a specified position in the ArrayList. |
getFirst() | Returns the first element of the ArrayList. (Introduced in Java 21) |
getLast() | Returns the last element of the ArrayList. (Introduced in Java 21) |
indexOf() | Returns the index of the first occurrence of a specified element. |
isEmpty() | Checks if the ArrayList is empty. |
iterator() | Returns an iterator to iterate over the elements of the ArrayList. |
lastIndexOf() | Returns the index of the last occurrence of a specified element. |
listIterator() | Returns a list iterator to iterate over the elements of the ArrayList. |
removeAll() | Removes all elements that are also in another collection. |
removeFirst() | Removes the first element of the ArrayList. (Introduced in Java 21) |
removeIf() | Removes all elements that satisfy a specified condition. |
removeLast() | Removes the last element of the ArrayList. (Introduced in Java 21) |
removeRange() | Removes elements between specified indices. |
retainAll() | Retains only the elements that are also in another collection. |
set() | Replaces the element at a specified position with a new element. |
spliterator() | Returns a Spliterator to traverse elements sequentially or in parallel. |
subList() | Returns a view of a portion of the ArrayList. |
toArray() | Converts the ArrayList to an array. |