dsa

Java ArrayList

In this tutorial, we will learn about the ArrayList, its methods and how to implement it in Java.

Arraylist class implements List interface and it is totally based on an Array data structure. It is mostly used because of the functionality and flexibility it offers.

Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When the objects are removed, the array may be shrunk.

Constructors in the ArrayList

To create an ArrayList we have to create objects of ArrayList which can be done by the constructors of the ArrayList.There are three constructors present in ArrayList :

  1. ArrayLIst(): This constructor is used to create an empty ArrayList.It can be created as:
    ArrayList arr = new ArrayList();
    pre>
  2. ArrayList(Collection c): This constructor is used to create an ArrayList having the elements of Collection c.
    ArrayList arr = new ArrayList(c);
    pre>
  3. ArrayList(int capacity): This constructor is used to create an ArrayList with the given initial capacity.
    ArrayList arr = new ArrayList(N);
    pre>

Note: You can create a generic ArrayList according to the data types provided.For Example:

ArrayList<String> arr=new ArrayList<String>

Methods of ArrayList

Apart from the methods inherited from its parent class/interface ,it has following methods :

MethodsDescription
void add(int index, Object element)It inserts the specified element at the specified position index in this list.
boolean add(Object o)It appends the specified element to the end of this list.
boolean addAll(Collection c)It appends all of the elements in the specified collection to the end of this list.
boolean addAll(int index, Collection c)It inserts all of the elements of the specified collection into this list, starting at the specified position.
void clear()It removes all of the elements from this list.
Object clone()It returns a shallow copy of this ArrayList.
boolean contains(Object o)It returns true if this list contains the specified element.
void ensureCapacity(int minCapacity)It ensures that it can hold at least the number of elements specified and increases the capacity of this ArrayList instance, if necessary .
Object get(int index)It returns the element at the specified position in this list.
int indexOf(Object o)It returns the index of the first occurrence of the specified element in this list, or -1 if the List does not contain this element.
int lastIndexOf(Object o)It returns the index of the last occurrence of the specified element in this list , or -1 if the list does not contain this element.
Object remove(int index)It removes the element at the specified position in this list.
protected void removeRange(int fromIndex, int toIndex)It removes all of the elements from this List whose index is between fromIndex(inclusive) and toIndex(exclusive).
Object set(int index, Object element)Replaces the element at the specified position in this list with the specified element.
int size()It returns the number of elements in this list.
Object[] toArray()It returns an array containing all of the elements in this list in the correct order.
Object[] toArray(Object[] a)It returns an array containing all of the elements in this list in the correct order.
void trimToSize()It trims the capacity of this ArrayList instance to be the list's current size.

Example


import java.util.*;
public class ArrayListTest 
{
   	public static void main(String args[]) 
	{
      		// creating an array list
      		ArrayList arr = new ArrayList();
      		System.out.println("Initial size of arr: " + arr.size());

      		// add elements to the array list
      		arr.add("A");
      		arr.add("B");
      		arr.add("C");
      		arr.add("D");
      		arr.add("E");
      		arr.add("F");
      		arr.add(1, "G");
      		System.out.println("Size of arr after additions: " + arr.size());

      		// display the array list
      		System.out.println("Contents of arr: " + arr);

      		// Remove elements from the array list
      		arr.remove("F");
      		arr.remove(2);
      		System.out.println("Size of arr after deletions: " + arr.size());
      		System.out.println("Contents of arr: " + arr);
   	}
}

Output:

Initial size of arr: 0
Size of arr after additions: 7
Contents of arr: [A, G, B, C, D, E, F]
Size of arr after deletions: 5
Contents of arr: [A, G, C, D, E]