w3resource logo


Introduction to Java Programming

Java.util.ArrayList Class

Secondary Nav

Introduction

public class ArrayList<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable

ArrayList Class represents a dynamically sized, index-based collection of objects. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list.

Constructor Summary :

Name Description
ArrayList() Constructs an empty list with an initial capacity of ten.
ArrayList(Collection<? extends E> c) Constructs a list containing the elements of the specified collection, in the order, they are returned by the collection's iterator.
ArrayList(int initialCapacity) Constructs an empty list with the specified initial capacity.

Method Summary :

Name Type Description
trimToSize() void Trims the capacity of this ArrayList instance to be the list's current size.
ensureCapacity(int minCapacity) void Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
size() int Provides the number of elements in an ArrayList object.
isEmpty() boolean Returns true if this list contains no elements.
contains(Object o) boolean Determines whether an element exists in an ArrayList object.
indexOf(Object o) int Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
lastIndexOf(Object o) int Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
clone() Object Creates a new instance of an ArrayList object that is a shallow copy of an existing ArrayList object.
toArray() Object[] Returns an array containing all of the elements in this list in proper sequence (from first to the last element).
toArray(T[] a) <T> T[] Returns an array containing all of the elements in this list in proper sequence (from first to the last element); the runtime type of the returned array is that of the specified array.
get(int index) E Returns the element at the specified position in this list.
set(int index, E element) E Replaces the element at the specified position in this list with the specified element.
add(E e) boolean Adds an element to the end of an ArrayList.
add(int index,E element) void Inserts the specified element at the specified position in this list.
remove(int index) E Removes the element at the specified position in this list.
remove(Object o) boolean Removes the first occurrence of the specified element from this list if it is present.
clear() void Removes all items from an ArrayList instance.
addAll(Collection<? extends E> c) boolean Appends all the items in an existing collection into an ArrayList object.
addAll(int index, Collection<? extends E> c) boolean Inserts all of the elements in the specified collection into this list, starting at the specified position.
removeRange(int fromIndex, int toIndex) protected void Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.
removeAll(Collection<?> c) boolean Removes from this list all of its elements that are contained in the specified collection.
retainAll(Collection<?> c) boolean Retains only the elements in this list that are contained in the specified collection.
listIterator(int index) ListIterator<E> Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
listIterator() ListIterator<E> Returns a list iterator over the elements in this list (in proper sequence).
iterator() Iterator<E> Returns an iterator over the elements in this list in proper sequence.
subList(int fromIndex, int toIndex) List<E> Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
forEach(Consumer<? super E> action) void Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.
spliterator() Spliterator<E> Creates a late-binding and fail-fast Spliterator over the elements in this list.
removeIf(Predicate<? super E> filter) boolean Removes all of the elements of this collection that satisfy the given predicate.
replaceAll(UnaryOperator<E> operator) void Replaces each element of this list with the result of applying the operator to that element.
sort(Comparator<? super E> c) void Sorts this list according to the order induced by the specified Comparator.


Join our Question Answer community to learn and share your programming knowledge.

Help the community:

PHP: Loop

JavaScript: Delete duplicates in an array

Oracle: Round the number