
A Java ArrayList is a growable array that grows dynamically according to the number of elements we add and insert elements. If we talk about memory, when we add or insert any element in ArrayList then it dynamically allocate and reallocate memory to hold all the elements.
The Arraylist inherits the methods from the List interface. You can access the elements of an ArrayList by using the get() and set() methods. ArrayList is not synchronized, so if multiple threads work simultaneously the result may be wrong in some cases.
In this article we will discuss about key points, constructors, methods available in ArrayList class, generic ArrayList and when you should and when you should not go for ArrayList. We will also discuss the difference between ArrayList and LinkedList.
Table of Contents
Basic points to remember about Java ArrayList
- It follows the insertion order and it can store duplicate elements, so you do not need to worry about the order of the elements.
- We can insert null value in the ArrayList, and we can have any number of null value.
- ArrayList implements RandomAccess interface so you can access random elements by using index.
- You can add Heterogeneous objects to the ArrayList. In other words, you can add any types of object into it, it may be Double type, String type, Integer type and so on.
Constructors of ArrayList
The following constructors are available for ArrayList, including a constructor without arguments, a constructor that accepts an initial capacity, and a constructor that accepts a collection:
ArrayList list = new ArrayList();
ArrayList list = new ArrayList(int initialCapacity);
ArrayList list = new ArrayList(Collection c);
Note:
Default initial size of ArrayList is 10.
Once ArrayList reaches it’s maximum capacity then a new ArrayList object will be created with
new capacity = current capacity * 3/2 + 1
Methods of ArrayList class
Here is the list of methods available in ArrayList class, by using these methods we store, insert, delete, retrieve elements from ArrayList.
# | Methods | Description |
1. | boolean add(E e) | This method adds elements to the end of the ArrayList, and returns true if successfully added. |
2. | void add(int position, E obj) | This method inserts element at the specified position. |
3. | E remove(int position) | This method removes an element from the specified position and then returns the removed element. |
4. | boolean remove(Object obj) | You can use this method to removes the specified object and then returns true/false. |
5. | void clear() | Once you call this method, the ArrayList will be empty. |
6. | boolean contains(Object obj) | This method returns true if ArrayList contains the specified element. |
7. | E get(int position) | It returns the element available at the specified position. |
8. | int size() | It returns the number of element available in the ArrayList. |
Example program using Java ArrayList
This is an example program of Java ArrayList in which we add four items and display the items of ArrayList on screen.
import java.util.ArrayList;
public class ArrayListProgram {
public static void main(String args[]) {
ArrayList arrayList = new ArrayList();
arrayList.add("Item 1");
arrayList.add("Item 2");
arrayList.add("Item 3");
arrayList.add("Item 4");
System.out.println("ArrayList: " + arrayList);
}
}
Output:
ArrayList: [Item 1, Item 2, Item 3, Item 4]
Generic ArrayList in Java
Generic ArrayList allows us to hold the elements of only one type that we specify. You can write the Generic ArrayList by using following syntax:
class ArrayList<E>
Here E represents the type of elements that ArrayList will contains. The following is an example of an ArrayList that can store objects of String type and Double type.
ArrayList<String> list = new ArrayList<String>();
ArrayList<Double> list = new ArrayList<Double>(30);
When should you use ArrayList?
If you need to get the elements from the list frequently then you should choose ArrayList. Because you can get element by providing index of the element which is faster.
When should you not use ArrayList?
If you insert or delete the elements frequently in the middle of list then you should not use ArrayList because during insertion and deletion it performs many shift operation.
Difference between ArrayList and LinkedList
There are following differences between ArrayList and LinkedList:
# | ArrayList | LinkedList |
1 | ArrayList uses a dynamic array to store the items. | LinkedList uses a doubly linked list to store the items. |
2 | ArrayList is slow because array manipulation is slower. | But LinkedList is faster because it is node based which does not require much shifting of items. |
3 | ArrayList implements List interface. | But LinkedList implements List as well as Queue. We can use LinkedList to implement Queue functionality. |
4 | ArrayList is best when we frequently store and access items. | LinkedList is best suitable when we frequently manipulate items. |
References:
https://docs.oracle.com/javase/7/docs/api/java/util/…
In conclusion, hope this article will help you to understand ArrayList class and we have tried to cover all basic information about ArrayList which can help you in interview as well.