ArrayList in Java With Example


We will cover following points of Arraylist in Java:

  1. Basics of Arraylist in Java
  2. How to iterate through Arraylist in Java
  3. Constructor and methods of Arraylist in Java

Basics of Arraylist in Java

  1. ArrayList is a class which extends AbstractList class and implements List, Serializable, Cloneable, Iterable, and RandomAccess interface.
  2. In ArrayList, we can have duplicate elements.
  3. ArrayList follow insertion order.
  4. ArrayList uses indexing representation to store the elements, so we can access elements randomly.
  5. We can have null as an element in ArrayList.
  6. None of the methods of ArrayList are synchronized.
  7. The size of ArrayList increase dynamically, the initial capacity of ArrayList is 10 when we insert 11th element the size will become 16. The new capacity will be calculated (initial size *3)/2 +1 i.e 16.

How to iterate Arraylist in Java

  1. Using iterator
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

public class ArrayListExample {
     public static void main(String[] args) {
            List<String> list = new ArrayList<>();
            list.add("Nadeem");
            list.add("Puneet");
            list.add("Shyam");
            list.add("Jagdish");
            list.add("Aakash");
     Iterator<String> it = list.iterator();
       while (it.hasNext()) {
         System.out.println(it.next());
       }
     }
}
Nadeem
Puneet
Shyam
Jagdish
Aakash
  1. Using Enumeration
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

public class ArrayListExample{
     public static void main(String[] args) {
            ArrayList<String> list = new ArrayList<>();
            list.add("Nadeem");
            list.add("Puneet");
            list.add("Shyam");
            list.add("Jagdish");
            list.add("Aakash");
     Enumeration<String> enm = list.elements();
       while (enm.hasMoreElements()) {
         System.out.println(it.next());
       }
     }
}
Nadeem
Puneet
Shyam
Jagdish
Aakash
  1. Using ListIterator
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

public class ArrayListExample {
     public static void main(String[] args) {
            ArrayList<String> list = new ArrayList<>();
            list.add("Nadeem");
            list.add("Puneet");
            list.add("Shyam");
            list.add("Jagdish");
            list.add("Aakash");
      ListIterator<String> listIter = list.listIterator();
       while (listIter .hasNext()) {
        System.out.println(listIter.next());
       }
     }
}
Nadeem
Puneet
Shyam
Jagdish
Aakash
  1. Using For-each loop
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

public class ArrayListExample {
     public static void main(String[] args) {
            ArrayList<String> list = new ArrayList<>();
            list.add("Nadeem");
            list.add("Puneet");
            list.add("Shyam");
            list.add("Jagdish");
            list.add("Aakash");
       for (String str : list) {
         System.out.println(list.get(i));
       }
     }
}
Nadeem
Puneet
Shyam
Jagdish
Aakash
  1. Using For loop
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

public class ArrayListExample {
     public static void main(String[] args) {
            List<String> list = new ArrayList<>();
            list.add("Nadeem");
            list.add("Puneet");
            list.add("Shyam");
            list.add("Jagdish");
            list.add("Aakash");
           for(int i = 0; i < list.size();i++) { 
            System.out.println(list.get(i)); 
           }
     }
}
Nadeem
Puneet
Shyam
Jagdish
Aakash
  1. Using Java 8 for-each expression
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

public class ArrayListExample {
     public static void main(String[] args) {
            List<String> list = new ArrayList<>();
            list.add("Nadeem");
            list.add("Puneet");
            list.add("Shyam");
            list.add("Jagdish");
            list.add("Aakash");
            list.forEach(str -> {
               System.out.println(str);
            });
     }
}
Nadeem
Puneet
Shyam
Jagdish
Aakash

Constructor and methods of Arraylist in Java

ArrayList in Java has mainly 3 constructor:

Learn more about collection in java


%d bloggers like this: