Java ArrayList

Java ArrayList Class

A Java ArrayList class is a dynamic array which is used to store the elements. It is a part of collection framework. It implements the List Interface and inherits the AbstractList class.

The benefit of using ArrayList over a standard array is that, in a standard array, we are able to store only a fixed number of elements. Java ArrayList also allows us to access the list randomly.

Java ArrayList comprises of both constructors and methods. Following are some constructors and methods along with their usage.

Constructors of ArrayList :

ArrayList() It creates an empty ArrayList with a default capacity of 10. Syntax: ArrayList<E> myArray= new ArrayList<E>();
ArrayList(Collection c) It creates a list containing the elements of the specified collection. Syntax: public boolean addAll(collection c)
ArrayList(int Capacity) It creates an empty ArrayList of specified capacity in the parameter. Syntax: ArrayList myArray= new ArrayList(int Capacity);

Example to use constructors:

Output:

Methods of Java ArrayList:

Modifier And Type Methods Usage
Boolean add (E a) It is used to add an element to the end of the list
addAll(Collection c) It is used to add all of the elements in the specified collection to the end of the list. And it is added in the order that they returned from the specified collection’s iterator.
addAll(int index, Collection c) It will insert all the elements at the specified index position in the specified collection into the list.
Contains(Object o) If the list contains the specified elements, it will return true.
isEmpty() If the list is blank, it will return true.
remove(Object o) If the specified element is present, then the first occurrence of that element will be removed from the list.
removeAll(Collection c) It will remove all the elements of the list from the specified collection.
retainAll(Collection c) It continues to hold only the elements in the list contained in the specified collection.
int indexof(Object o) It will return -1 if the list is blank or returns the index number for the first occurrence of the specified component in the list.
lastIndexOf(Object o) It will return the index of the last occurrence of the specified element in the list, or -1 if the list is empty.
size() It gives the number of elements in the list.
void add(int index, E element) It is used to insert a specific element at the specified position in the ArrayList.
clear() It will remove all elements from the list.
ensureCapacity(int minCapacity) The capacity of the ArrayList will be increased to ensure that it holds the minimum number of elements specified in the minCapacity argument.
trimToSize() It will trim the capacity of the ArrayList to the list’s current size.
Object[ ] clone() It will return a shallow copy of the ArrayList instance.
toArray() Will return an array containing all elements in the list in proper sequence(First to Last).
Iterator<E> iterator() It will return an iterator over the element in the list in the sequence.
ListIterator<E> listIterator() Will return a listIterator over the element in the list(in sequence)
listIterator(int index) Will return a listIterator over the element in the list from the specified index.
List<E> subList(int fromIndex, int toIndex) Returns the element from the list between the specified index value.
abstract<E> get(int index) It will return the element from the specified index in the list.
E remove(int index) It removes the element from the specified position in the list.
E set(int index, E element) It will replace the element from the specified position in the list with the specified element in the argument.

Advantages of ArrayList over Array:

  • The length of the ArrayList is variable.
  • The size of the ArrayList can be modified dynamically.
  • The ArrayList can traverse in both directions.
  • We can add different types of data in an ArrayList if we do not use generics.

Example to add, remove, clone, and clear the elements in the ArrayList.

Output:

Example to illustrate addAll(Collection c)

Output:

Example to illustrate addAll(int index, Collection c)

Output:

Example to demonstrate the working of contains() method in ArrayList.

Output :

Example to demonstrate the working of isEmpty() method in ArrayList

Output:

Example to demonstrate the working of indexOf in ArrayList

Output:

Example to demonstrate the working of  lastIndexOf() and size() method in ArrayList.

Output:

Example to illustrate add(int index, Object element):

Output:

Example to demonstrate  ensureCapacity() method:

Output:

Example to demonstrate the working of  trimTosize() method in ArrayList.

Output:

Example to demonstrate working of Objectp[] toArray().

Output:

Example to demonstrate listIterator() method

Output:

Example to demonstrate subList() method

Output:

Example to demonstrate the working of  get() method in ArrayList

Output:

Example to demonstrate set() method

Output: