Java ArrayList

Java ArrayList

Java ArrayList

The ArrayList is just like an array but it’s size can grow at run time. It is an ordered collection by index but elements are not sorted. ArrayList provides faster iteration because elements are ordered. ArrayList is good when you need fast iteration.

The initial size of ArrayList is 10. This capacity increases automatically as new elements inserted. We can also specify the initial capacity as per our requirement.

ArrayList implements List interface along with three marker interfaces RandomAccess, Cloneable and Serializable. It also extends AbstractList class.


Some important points about ArrayList


  1. The size of ArrayList can increase and decrease at run time.
  2. ArrayList can have duplicate elements.
  3. ArrayList can contain number of null elements.
  4. Elements in ArrayList are placed on the basis of Zero based index and ordered accordingly.
  5. ArrayList implements RandomAccess so you can set, get, remove and add elements at any arbitrary position.
  6. ArrayList is not synchronized so multiple threads can access ArrayList at the same time.

ArrayList class hierarchy

Let’s see the internal hierarchy of ArrayList class which interface and classes implemented or extending by this class.


Java ArrayList

 

 



ArrayList Methods

Method return signature Method name and description
boolean add(E e)
Will add the element at the end of the list.
void add(int index, E element)
You can add the element at specific position in the list.
boolean contains(Object o)
Used to check the object existence in the list.
void ensureCapacity(int minCapacity)
Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
E get(int index)
Used to access the object from the list.
Iterator<E> iterator()
Returns the iterator instance to iterate over the list.
E remove(int index)
Removes the object from the list.
E set(int index, E element)
Set the value of the object at specific position.
int size()
Returns the current size of the ArrayList.
List<E> subList(int fromIndex, int toIndex)
Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
Object[] toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).



Example on ArrayList

Let’s implement some of the ArrayList example to know how it works in java programming. ArrayList can be used to add, retrieve, set, get and iterate the objects. It provides number of methods to perform these operations.

package com.test;

import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListTest {
 
 public static void main(String arg[]) {
 
   ArrayList<String> arrayList = new ArrayList<String>();
 
   arrayList.add("Welcome");
   arrayList.add(" to ");
   arrayList.add(" ArrayList.");
 
   System.out.println(arrayList);
 
   arrayList.set(2, " ArrayList example.");
 
   System.out.println(arrayList);
 
   arrayList.remove(1);
 
   System.out.println(arrayList);
 
   Iterator<String> it = arrayList.iterator();
 
   while(it.hasNext()) {
     System.out.println("Value : " + it.next());
   }
 }
}

Related Post

Leave a Reply

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