This article provides a comprehensive overview of ArrayList in Java programming language along with example programs.
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:
import java.util.*; public class Driver { public static void main(String[] args) { ArrayList<String> friends = new ArrayList<String>(); friends.add("Ramesh"); friends.add("Mahesh"); friends.add("Suresh"); friends.add("Dinesh"); friends.add("Ganesh"); for(String item : friends) { System.out.println(item); } } } Output for the above program is as follows: Ramesh Mahesh Suresh Dinesh Ganesh
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Java collections APIs provides lots of inbuilt classes and interfaces to handle collections of objects. It is very essential to learn and master java collections concepts. It is very important part of core java tutorial.
nice article for beginners.thank you.
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!