ArrayList in Java

ArrayList is a part of collection framework and is present in java.util package. It provides us dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed.

  • ArrayList inherits AbstractList class and implements List interface.
  • ArrayList is initialized by a size, however the size can increase if collection grows or shrunk if objects are removed from the collection.
  • Java ArrayList allows us to randomly access the list.
  • ArrayList can not be used for primitive types, like int, char, etc. We need a wrapper class for such cases (see this for details).
  • ArrayList in Java can be seen as similar to vector in C++.


Now primarily the Java ArrayList can constitute of both Constructors and Methods. Below mentioned is a list of few constructors and methods along with there use and functions.

Constructors in Java ArrayList:

  1. ArrayList(): This constructor is used to build an empty array list
  2. ArrayList(Collection c): This constructor is used to build an array list initialized with the elements from collection c
  3. ArrayList(int capacity): This constructor is used to build an array list with initial capacity being specified

Let us look at the code to create generic ArrayList-

// Creating generic integer ArrayList
ArrayList<Integer> arrli = new ArrayList<Integer>();
// Java program to demonstrate working of ArrayList in Java
import java.util.*;

class arrayli
    public static void main(String[] args)
                       throws IOException
        // size of ArrayList
        int n = 5;

        //declaring ArrayList with initial size n
        ArrayList<Integer> arrli = new ArrayList<Integer>(n);

        // Appending the new element at the end of the list
        for (int i=1; i<=n; i++)

        // Printing elements

        // Remove element at index 3

        // Displaying ArrayList after deletion

        // Printing elements one by one
        for (int i=0; i<arrli.size(); i++)
            System.out.print(arrli.get(i)+" ");


[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5 

Methods in Java ArrayList:

  1. void clear(): This method is used to remove all the elements from any list.
  2. void add(int index, Object element): This method is used to insert a specific element at a specific position index in a list.
  3. void trimToSize(): This method is used to trim the capacity of the instance of the ArrayLis to the list’s current size.
  4. int indexOf(Object O): The index the first occurrence of a specific element is either returned, or -1 in case the element is not in the list.
  5. int lastIndexOf(Object O): The index the last occurrence of a specific element is either returned, or -1 in case the element is not in the list.
  6. Object clone(): This method is used to return a shallow copy of an ArrayList.
  7. Object[] toArray(): This method is used to return an array containing all of the elements in the list in correct order.
  8. Object[] toArray(Object[] O): It is also used to return an array containing all of the elements in this list in the correct order same as the previous method.
  9. boolean addAll(Collection C): This method is used to append all the elements from a specific collection to the end of the mentioned list, in such a order that the values are returned by the specified collection’s iterator.
  10. boolean add(Object o): This method is used to append a specificd element to the end of a list.
  11. boolean addAll(int index, Collection C): Used to insert all of the elements starting at the specified position from a specific collection into the mentioned list.

Also see – Array vs ArrayList in Java

This article is contributed by Bharat Sahni. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up

Practice Tags :

Recommended Posts:

2 Average Difficulty : 2/5.0
Based on 29 vote(s)

User Actions