Java ArrayList Class Methods

The ArrayList class in Java is a resizable array, which can be found in the java.util package. Unlike an array that has a fixed length, an ArrayList can grow or shrink in size. It is one of the most commonly used data structures for managing dynamic collections of objects.

This guide provides an overview of the various methods available in the ArrayList class. Each method is explained in simple terms, making it easy for beginners to understand how to use them. The methods allow you to add, remove, search, and iterate over elements in the list.

For more detailed information, you can refer to the official Java SE Documentation and additional resources on Java Collections Tutorial.

Learn everything about ArrayList at Java ArrayList Tutorial with Examples

Java ArrayList Class Methods

The table below contains various methods of the Java ArrayList class, each with a link to a detailed explanation, examples, and real-world uses. Click on the method names to learn more about how to use them effectively in your applications.

Method Description
add() Adds an element to the list. Returns true if the element is added successfully.
addAll() Adds all elements from a collection to the list. Returns true if the list is changed.
addFirst() Adds an element to the beginning of the list.
addLast() Adds an element to the end of the list.
clear() Removes all elements from the list.
clone() Creates a shallow copy of the list.
contains() Checks if the list contains the specified element. Returns true if found.
ensureCapacity() Increases the capacity of the list to ensure it can hold a specified number of elements.
equals() Compares the specified object with the list for equality.
forEach() Performs the given action for each element of the list.
get() Returns the element at the specified position in the list.
indexOf() Returns the index of the first occurrence of the specified element in the list, or -1 if not found.
isEmpty() Checks if the list is empty. Returns true if the list is empty.
iterator() Returns an iterator over the elements in the list.
lastIndexOf() Returns the index of the last occurrence of the specified element in the list, or -1 if not found.
listIterator() Returns a list iterator over the elements in the list.
remove() Removes the element at the specified position in the list and returns it.
removeAll() Removes all elements from the list that are contained in the specified collection. Returns true if the list is changed.
removeFirst() Removes and returns the first element from the list.
removeLast() Removes and returns the last element from the list.
retainAll() Retains only the elements in the list that are contained in the specified collection. Returns true if the list is changed.
spliterator() Creates a Spliterator over the elements in the list.
subList() Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
toArray() Returns an array containing all of the elements in the list in proper sequence.
trimToSize() Trims the capacity of this ArrayList instance to be the list's current size.

Comments