Java Three programs to Iterate a Arraylist . Here are three different ways to iterate through an ArrayList in Java, each with a step-by-step explanation:
Method 1: Using a for-loop
java import java.util.ArrayList; public class ArrayListIterationExample { public static void main(String[] args) { // Create an ArrayList of Strings ArrayList<String> list = new ArrayList<>(); list.add("MEEERUT"); list.add("LONDON"); list.add("USA"); // Iterate using a for-loop System.out.println("Iterating using a for-loop:"); for (int ivar = 0; ivar < list.size(); ivar ++) { String element = list.get(ivar ); System.out.println(element); } } }
Explanation:
1. ArrayList Creation: We first create an ArrayList named list and add three elements (“Apple”, “Banana”, “Cherry”).
2. For-loop: We use a standard for loop to iterate through the ArrayList.
– list.size() gives the number of elements in the ArrayList.
– list.get(i) retrieves the element at index i.
– System.out.println(element) prints each element.
Method 2: Using an enhanced for-loop (for-each loop)
java import java.util.ArrayList; public class ArrayListIteration_Example { public static void main(String[] args) { // Create an ArrayList of Strings ArrayList<String> list = new ArrayList<>(); list.add("mahesh"); list.add("Suresh"); list.add("Naman"); // Iterate using an enhanced for-loop System.out.println("Iterat using an for-loop:"); for (String elements : list) { System.out.println(elements); } } }
Explanation:
1. ArrayList Creation: Same as before, we create an ArrayList named list.
2. Enhanced for-loop: This loop simplifies iteration by directly accessing each element of the ArrayList.
– for (String element : list) iterates through each element in list.
– element represents the current element in each iteration.
– System.out.println(element) prints each element.
Method 3: Using Iterator
java import java.util.ArrayList; import java.util.Iterator; public class ArrayListIteration_Example { public static void main(String[] args) { // Create an ArrayList of Strings ArrayList<String> list = new ArrayList<>(); list.add("Ritu"); list.add("Neha"); list.add("Jully"); // Iterate using an Iterator System.out.println("Iterating using an Iterator Example Java:"); Iterator<String> iterator = list.iterator(); while (iterator.hasNext()) { String ele = iterator.next(); System.out.println(ele); } } }
Explanation:
1. ArrayList Creation: Again, we create an ArrayList named list.
2. Iterator: We obtain an iterator using list.iterator().
– iterator.hasNext() checks if there are more elements to iterate over.
– iterator.next() returns the next element in the ArrayList.
– System.out.println(element) prints each element.
Summary of Steps:
– Create ArrayList: Initialize an ArrayList and add elements to it.
– For-loop: Use a standard for loop to iterate over the ArrayList by index.
– Enhanced for-loop: Utilize a simplified for-each loop to iterate directly over elements.
– Iterator: Obtain an iterator and use it to traverse through the ArrayList.