Java Utility Library

Java.util.ArrayList Class



Java ArrayList Class

Java.util package provides an ArrayList class which provides resizable-array implementation of the list interface. It implements all optional list operations and permits all elements including null. Along with this, the class provides methods to manipulate the size of the array that is used internally to store the list. This class is almost equivalent to Vector class except the implementation is not synchronized.

Class declaration

The declaration of java.util.ArrayList class is:

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

Here, E is the type of element maintained by the container.

Class Constructors

S.NConstructors & Description
1. ArrayList()
Creates an empty list with initial capacity to hold 10 elements.
2. ArrayList(Collection<? extends E> c)
Creates a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
3. ArrayList(int initialCapacity)
Creates an empty list with the specified initial capacity.

java.util.ArrayList Methods

The java.util.ArrayList class has a number of methods which are listed below:

Member Methods

S.NMethods & Description
1. boolean add(E element)
Adds a new element at the end of the ArrayList.
2. void add(int index, E element)
Inserts the specified element at the specified position in the ArrayList.
3. boolean addAll(Collection<? extends E> c)
Appends all elements of the specified collection at the end of the ArrayList.
4. boolean addAll(int index, Collection<? extends E> c)
Inserts all elements of the specified collection at the specified position in the ArrayList.
5. void clear()
Clears all elements of the ArrayList.
6. Object clone()
Returns a shallow copy of this ArrayList instance.
7. boolean contains(Object obj)
Returns true if the list contains the specified element.
8. void ensureCapacity(int minCapacity)
Increases the capacity of the ArrayList instance, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument.
9. void forEach(Consumer<? super E> action)
Performs the given action for each element of the iterable until all elements have been processed or the action throws an exception.
10. E get(int index)
Returns the element at specified index of the ArrayList.
11. int indexOf(Object o)
Returns the index of the first occurrence of the specified element in the ArrayList, or -1 if element is not present in the ArrayList.
12. boolean isEmpty()
Returns true if the ArrayList is empty, else returns false.
13. Iterator<E> iterator()
Returns an iterator over the elements in the list in proper sequence.
14. int lastIndexOf(Object obj)
Returns the index of the last occurrence of the specified element in the ArrayList, or -1 if element is not present in the ArrayList.
15. ListIterator<E> listIterator()
Returns a list iterator over the elements in the list (in proper sequence).
16. ListIterator<E> listIterator(int index)
Returns a list iterator over the elements in the list (in proper sequence), starting at the specified position in the list.
17. E remove(int index)
Removes the element at the specified position in the list.
18. boolean remove(Object obj)
Removes the first occurrence of the specified element from the list, if it is present.
19. boolean removeAll(Collection<?> c)
Removes from the list all of its elements that are contained in the specified collection.
20. boolean removeIf(Predicate<? super E> filter)
Removes all of the elements of the collection that satisfy the given predicate.
21. protected void removeRange(int fromIndex, int toIndex)
Removes from the list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.
22. void replaceAll(UnaryOperator<E> operator)
Replaces each element of the list with the result of applying the operator to that element.
23. boolean retainAll(Collection<?> c)
Retains only the elements in the vector that are contained in the specified collection.
24. E set(int index, E element)
Replaces the element at the specified index in the ArrayList with the specified element.
25. int size()
Returns the number of elements in the ArrayList.
26. void sort(Comparator<? super E> c)
Sorts the elements of the list according to the order induced by the specified comparator.
27. Spliterator<E> spliterator()
Creates a late-binding and fail-fast spliterator over the elements in the list.
28. List<E> subList(int fromIndex, int toIndex)
Returns a view of the portion of the list between fromIndex, inclusive, and toIndex, exclusive.
29. Object[] toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).
30. <T> T[] toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.
31. void trimToSize()
Trims the capacity of the ArrayList instance to be the list's current size.

Methods inherited

This class inherits the methods of following class:

  • java.lang.Object
  • java.util.AbstractCollection<E>
  • java.util.AbstractList<E>