How ArrayList Class In Java Collections Are Used

Introduction

 
This article describes how the ArrayList class in Java Collections work.
 

What is the ArrayList class in Java?

 
Java arrays are of a fixed length. When arrays are created, they are unable to grow or shrink, in other words, we must know in advance how many elements we need so that the array will hold them. Now, the ArrayList class extends Array-List and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.
 
This class supports mainly three constructors.
ArrayList(int capacity)
Apart from the methods inherited from its parent classes, ArrayList defines the following methods.
 
ArrayList( )
This constructor builds an array list that is initialized with the elements of the collection cln.
 
ArrayList(Collection cln)
This constructor builds an array list that has the specified initial capacity. The capacity grows automatically as elements are added to an array list.
Example: The following example contains various methods used by the ArrayList class.
  1. import java.util.*;  
  2. public class ArrayListEx1 {  
  3.  public static void main(String args[]) {  
  4.   ArrayList arylst = new ArrayList();  
  5.   System.out.println("size of initialized arraylist: " + arylst.size());  
  6.   arylst.add("C");  
  7.   arylst.add("P");  
  8.   arylst.add("Q");  
  9.   arylst.add("R");  
  10.   arylst.add("S");  
  11.   arylst.add("T");  
  12.   arylst.add(1"P2");  
  13.   System.out.println("Size of arraylist after additions: " + arylst.size());  
  14.   System.out.println("Contents of arraylist: " + arylst);  
  15.   arylst.remove("T");  
  16.   arylst.remove(2);  
  17.   System.out.println("After deletion size of arraylist: " + arylst.size());  
  18.   System.out.println("The contents of arraylist: " + arylst);  
  19.  }  
  20. }  
Output
 
Fig-1.jpg 
The following are two ways to iterate the elements of the collection:
  1. By Iterator interface.
  2. By For-each loop

1. By using a For-each loop

 
Example
  1. import java.util.*;  
  2. class ArrayListEx2 {  
  3.  public static void main(String args[]) {  
  4.   ArrayList arylst = new ArrayList();  
  5.   arylst.add("John");  
  6.   arylst.add("Paul");  
  7.   arylst.add("Jaun");  
  8.   arylst.add("San");  
  9.   for (Object obj: arylst)  
  10.    System.out.println(obj);  
  11.  }  
  12. }   
Output
 
Fig-2.jpg

2. By using the Iterator interface

 
Example
  1. import java.util.*;  
  2. class ArrayListEx4 {  
  3.  public static void main(String args[]) {  
  4.   ArrayList arylst = new ArrayList();  
  5.   arylst.add("Paul");  
  6.   arylst.add("John");  
  7.   arylst.add("Paul");  
  8.   arylst.add("San");  
  9.   Iterator itrtr = arylst.iterator();  
  10.   while (itrtr.hasNext()) {  
  11.    System.out.println(itrtr.next());  
  12.   }  
  13.  }  
  14. }  
Output
 
Fig-4.jpg
Storing user-defined class objects.
 
Chilgren.java: This class contains Student records, such as their name, age, and rollno.
  1. class Children {  
  2.  int rollno;  
  3.  String name;  
  4.  int age;  
  5.  Children(int rollno, String name, int age) {  
  6.   this.rollno = rollno;  
  7.   this.name = name;  
  8.   this.age = age;  
  9.  }  
  10. }  
ArrayListEx3.java
 
This class is used to show or print the records of the Children defined class.
  1. import java.util.*;  
  2. class ArrayListEx3 {  
  3.  public static void main(String args[]) {  
  4.   Children c1 = new Children(11"Paul"21);  
  5.   Children c2 = new Children(12"Sam"23);  
  6.   Children c3 = new Children(13"Cady"22);  
  7.   ArrayList arylst = new ArrayList();  
  8.   arylst.add(c1);  
  9.   arylst.add(c2);  
  10.   arylst.add(c3);  
  11.   Iterator it = arylst.iterator();  
  12.   while (it.hasNext()) {  
  13.    Children ch = (Children) it.next();  
  14.    System.out.println(ch.rollno + "  " + ch.name + "  " + ch.age);  
  15.   }  
  16.  }  
  17. }  
Output
 
fig-3.jpg