Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

February 20, 2018 Categories: Java Collections. 2 Comments on ArrayList in Java

Introduction to ArrayList


ArrayList class uses a dynamic array for storing a list of elements. It is inherited from AbstractList class and implements List interface. Following are some important points about ArrayList:


  • ArrayList can contain duplicate elements.
  • ArrayList size varies. When number of elements increase, size of ArrayList increases automatically, and when number of elements decrease, size of ArrayList also decreases.
  • ArrayList preserves the insertion order of elements.
  • ArrayList class is non-synchronized.
  • ArrayList allows random access of elements as it uses an array to store elements.
  • ArrayList is slow when insertion or deletion of elements is performed.


ArrayList Constructors


Following constructors are supported by the ArrayList class:


Constructor Description
ArrayList( ) Build an empty array list
ArrayList(Collection c) Builds an array list with the given collection c
ArrayList(int capacity) Builds an array list with specified initial capacity


ArrayList Methods


Following are some of the most frequently used methods of ArrayList:


Method Description
boolean add(Object o) Add the given element at the end of the list
void add(int index, Object element) Used to insert given element at the specified index in a list.
boolean addAll(Collection c) Add all the elements in the given collection c at the end of the list.
boolean addAll(int index, Collection c) Add the elements in the given collection at the end of the list.
void clear( ) Clear all the elements in the list and make it empty.
Object clone( ) Returns a shallow copy of the list
int indexOf(Object o) Returns the index of the given element on the list. Otherwise, returns -1 if the element is not present in the list.
int lastIndexOf(Object o) Returns the index of last occurrence of the given element in the list. Otherwise, returns -1 if the element is not present in the list.
Object[ ] toArray() Converts all the elements in the list to an array.
Object[ ] toArray(Object[ ] a) Converts all of the elements in this list in the correct order.


ArrayList Example


Following is an example program which demonstrates adding elements to a ArrayList and displaying them using a for  each loop:



Related Links:

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.


You can follow any responses to this entry through the RSS 2.0 feed.

Great post! The whole article about ArrayList were explained really nicely and as beginner it’s very easy to understand for me as well as others. keep up the great work!

nice article for beginners.thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up