ArrayList Class In Java
ArrayList Class
In Java collections, ArrayList class uses a dynamic array to store the elements. It extends AbstractList class and implements List interface. This class can contain the duplicate elements and maintains insertion order. ArrayList class is non-synchronized and allows the random access because an array works at the index basis. Its manipulation is very slow because a lot of shifting requires to be occurred, if any element is removed from the array list.
Non-generic Vs Generic Collection
Before JDK 1.5, collection framework was non-generic.
Since 1.5, it is generic and generic collection allows only one type of object in the collection. It is type safe. Thus, typecasting is not required at the run time. In generic, we can specify the type in angular braces. Now, ArrayList is forced to have only particular type of objects in it. If we try to add another type of the object, it gives the compile time error.
Let's see the non-generic example, given below.
ArrayList a1=new ArrayList();
Let's see the generic example, given below.
ArrayList<String> a1=new ArrayList<String> ();
Constructors of ArrayList class
ArrayList( )
It builds an empty array list.
ArrayList(Collection c)
It builds an array list, which is initialized with the elements of the collection c.
ArrayList(int capacity)
It builds an array list, which has the specified initial capacity. The capacity is the size of the underlying array, which is used to store the elements and capacity grows automatically.
Methods of ArrayList class
void add(int index, Object element)
This method is used to Insert the specified element at the specified position index in this list. It throws IndexOutOfBoundsException, if the specified index is out of range.
boolean add(Object o)
This method is used to append the specified element to the end of this list.
boolean addAll(Collection c)
This method is used to append all of the elements in the specified collection to the end of this list in the order in which they are returned by the particular collection's iterator.
boolean addAll(int index, Collection c)
This method is used to Insert all of the elements in the particular collection into this list, starting at the specified position.
void clear()
This method is used to remove all the elements from the list.
Object clone()
This method is used to return a shallow copy of this ArrayList.
boolean contains(Object o)
This method is used to return true, if this list contains the particular element.
void ensureCapacity(int minCapacity)
This method is used to increase the capacity of the ArrayList object, if necessary, to ensure that it can hold at least the number of elements; particularly by the minimum capacity argument.
Object get(int index)
This method is used to return the element at the specified position in this list.
int indexOf(Object o)
This method is used to return the index in this list of the first occurrence of the specified element.
int lastIndexOf(Object o)
This method is used to return the index in this list of the last occurrence of the specified element.
Object remove(int index)
This method is used to remove the element at the specified position in this list.
protected void removeRange(int fromIndex, int toIndex)
This method is used to remove from the list of all of the elements, whose index is between from Index.
Object set(int index, Object element)
This method is used to replace the element at the particular position in this list with the particular element.
int size()
This method is used to return the number of the elements in this list.
Object[] toArray()
This method is used to return an array, which contains all the elements in this list in the correct order.
Object[] toArray(Object[] a)
This method is used to return an array, which contains all the elements in this list in the correct order and the runtime type of the returned array is that of the specified array.
void trimToSize()
This method is used to trim the capacity of the ArrayList object to be the list's current size.
There are two ways to iterate the elements of collection, given below.
- By Iterator interface.
- By for-each loop.
Let’s see an example “with iterator interface”, given below.
Code
- import java.util.*;
- public class CollectionsExample {
- public static void main(String args[]) {
- ArrayList < String > a1 = new ArrayList < String > ();
- a1.add("Harry");
- a1.add("Marria");
- a1.add("David");
- a1.add("Harry");
- Iterator i = a1.iterator();
- while (i.hasNext()) {
- System.out.println(i.next());
- }
- }
- }
Output
In the example, shown above, we can see that traversing ArrayList by Iterator.
Now, let’s see another example “with for-each loop”, given below.
Code
- import java.util.*;
- public class CollectionsExample {
- public static void main(String args[]) {
- ArrayList < String > a1 = new ArrayList < String > ();
- a1.add("Harry");
- a1.add("Marria");
- a1.add("David");
- a1.add("Harry");
- for (String s: a1) {
- System.out.println(s);
- }
- }
- }
Output
Let’s see the example of User-defined class objects in ArrayList, given below.
Code
- import java.util.*;
- public class CollectionsExample {
- public static void main(String args[]) {
- Employee e1 = new Employee(1051, "Mia", 23, 40000);
- Employee e2 = new Employee(1092, "Sophia", 21, 56000);
- Employee e3 = new Employee(1023, "Harry", 25, 58000);
- ArrayList < Employee > a1 = new ArrayList < Employee > ();
- a1.add(e1);
- a1.add(e2);
- a1.add(e3);
- Iterator i = a1.iterator();
- while (i.hasNext()) {
- Employee emp = (Employee) i.next();
- System.out.println(emp.empId + " " + emp.empName + " " + emp.empAge + " " + emp.empSalary);
- }
- }
- }
Output
Let’s see an example of addAll(Collection c) method
Code
- import java.util.*;
- public class CollectionsExample {
- public static void main(String args[]) {
- ArrayList < String > a = new ArrayList < String > ();
- a.add("George");
- a.add("James");
- a.add("Daniel");
- ArrayList < String > a1 = new ArrayList < String > ();
- a1.add("Lucy");
- a1.add("Bob");
- a.addAll(a1);
- Iterator itr = a.iterator();
- while (itr.hasNext()) {
- System.out.println(itr.next());
- }
- }
- }
Output
Let’s see an example of removeAll() method, given below.
Code
- import java.util.*;
- public class CollectionsExample {
- public static void main(String args[]) {
- ArrayList < String > a = new ArrayList < String > ();
- a.add("George");
- a.add("James");
- a.add("Daniel");
- ArrayList < String > a1 = new ArrayList < String > ();
- a1.add("Lucy");
- a1.add("Bob");
- a.removeAll(a1);
- System.out.println("After removing the elements of a1");
- Iterator itr = a.iterator();
- while (itr.hasNext()) {
- System.out.println(itr.next());
- }
- }
- }
Output
Let’s see an example of retainAll() method, give below.
Code
- import java.util.*;
- public class CollectionsExample {
- public static void main(String args[]) {
- ArrayList < String > a = new ArrayList < String > ();
- a.add("George");
- a.add("James");
- a.add("Bob");
- ArrayList < String > a1 = new ArrayList < String > ();
- a1.add("Lucy");
- a1.add("Bob");
- a.retainAll(a1);
- System.out.println("After retaining the elements of a1");
- Iterator itr = a.iterator();
- while (itr.hasNext()) {
- System.out.println(itr.next());
- }
- }
- }
Output
Summary
Thus, we learnt that Java collections, ArrayList class use a dynamic array to store the elements. It extends AbstractList class and implements the list interface. This class can contain duplicate elements and maintains insertion order and also learnt how to create it in Java.