INTRODUCTION TO JAVA ARRAYLIST
- Resizable-array implementation of the List interface. A java ArrayList is used to store an “ordered” group of elements where duplicates are allowed.
- Implements all optional list operations, and permits all elements, including null.
- This class is similar to Vector, except that it is unsynchronized.
- The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. ArrayList’s give great performance on get() and set() methods, but do not perform well on add() and remove() methods when compared to a LinkedList.
- An ArrayList capacity is the size of the array used to store the elements in the list. As elements are added to an ArrayList, its capacity grows automatically. It is an Array based implementation where elements of the List can be accessed directly through get() method.
public class ArrayList<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable
To prevent unsynchronized access to the list: List list = Collections.synchronizedList(new ArrayList(…));
JAVA ARRAYLIST EXAMPLE
Below is an ArrayList Example showing how collections are manipulated using an ArrayList
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Collections;
import java.util.Random;
public class ArrayListExample {
public static void main(String[] args) {
// ArrayList Creation
List arraylistA = new ArrayList();
List arraylistB = new ArrayList();
// Adding elements to the ArrayList
for (int i = 0; i < 5; i++) {
arraylistA.add(new Integer(i));
}
arraylistB.add("beginner");
arraylistB.add("java");
arraylistB.add("tutorial");
arraylistB.add(".");
arraylistB.add("com");
arraylistB.add("java");
arraylistB.add("site");
// Iterating through the ArrayList to display the Contents.
Iterator i1 = arraylistA.iterator();
System.out.print("ArrayList arraylistA --> ");
while (i1.hasNext()) {
System.out.print(i1.next() + " , ");
}
System.out.println();
System.out.print("ArrayList arraylistA --> ");
for (int j = 0; j < arraylistA.size(); j++) {
System.out.print(arraylistA.get(j) + " , ");
}
System.out.println();
Iterator i2 = arraylistB.iterator();
System.out.println("ArrayList arraylistB --> ");
while (i2.hasNext()) {
System.out.print(i2.next() + " , ");
}
System.out.println();
System.out.println();
System.out
.println("Using ListIterator to retrieve ArrayList Elements");
System.out.println();
ListIterator li1 = arraylistA.listIterator();
// next(), hasPrevious(), hasNext(), hasNext() nextIndex() can be used with a
// ListIterator interface implementation
System.out.println("ArrayList arraylistA --> ");
while (li1.hasNext()) {
System.out.print(li1.next() + " , ");
}
System.out.println();
// Searching for an element in the ArrayList
int index = arraylistB.indexOf("java");
System.out.println("'java' was found at : " + index);
int lastIndex = arraylistB.lastIndexOf("java");
System.out.println("'java' was found at : " + lastIndex
+ " from the last");
System.out.println();
// Getting the subList from the original List
List subList = arraylistA.subList(3, arraylistA.size());
System.out.println("New Sub-List(arraylistA) from index 3 to "
+ arraylistA.size() + ": " + subList);
System.out.println();
// Sort an ArrayList
System.out.print("Sorted ArrayList arraylistA --> ");
Collections.sort(arraylistA);
System.out.print(arraylistA);
System.out.println();
// Reversing an ArrayList
System.out.print("Reversed ArrayList arraylistA --> ");
Collections.reverse(arraylistA);
System.out.println(arraylistA);
System.out.println();
// Checking emptyness of an ArrayList
System.out.println("Is arraylistA empty? "
+ arraylistA.isEmpty());
System.out.println();
// Checking for Equality of ArrayLists
ArrayList arraylistC = new ArrayList(arraylistA);
System.out.println("arraylistA.equals(arraylistC)? "
+ arraylistA.equals(arraylistC));
System.out.println();
// Shuffling the elements of an ArrayList in Random Order
Collections.shuffle(arraylistA, new Random());
System.out
.print("ArrayList arraylistA after shuffling its elements--> ");
i1 = arraylistA.iterator();
while (i1.hasNext()) {
System.out.print(i1.next() + " , ");
}
System.out.println();
System.out.println();
// Converting an ArrayList to an Array
Object[] array = arraylistA.toArray();
for (int i = 0; i < array.length; i++) {
System.out.println("Array Element [" + i + "] = " + array[i]);
}
System.out.println();
// Clearing ArrayList Elements
arraylistA.clear();
System.out.println("arraylistA after clearing : " + arraylistA);
System.out.println();
}
}
Output
ArrayList arraylistA –> 0 , 1 , 2 , 3 , 4 ,
ArrayList arraylistA –> 0 , 1 , 2 , 3 , 4 ,
ArrayList arraylistB –>
beginner , java , tutorial , . , com , java , site ,
Using ListIterator to retrieve ArrayList Elements
ArrayList arraylistA –>
0 , 1 , 2 , 3 , 4 ,
‘java’ was found at : 1
‘java’ was found at : 5 from the last
New Sub-List(arraylistA) from index 3 to 5: [3, 4]
Sorted ArrayList arraylistA –> [0, 1, 2, 3, 4] Reversed ArrayList arraylistA –> [4, 3, 2, 1, 0]
Is arraylistA empty? false
arraylistA.equals(arraylistC)? true
ArrayList arraylistA after shuffling its elements–> 3 , 2 , 1 , 0 , 4 ,
Array Element [0] = 3
Array Element [1] = 2
Array Element [2] = 1
Array Element [3] = 0
Array Element [4] = 4
arraylistA after clearing : []
Download ArrayListExample.java