Code Example: ArrayList, Iteration, Removal, and Modification with Iterators

This example demonstrates how to create an ArrayList, iterate through its elements using an Iterator, and modify the list with a ListIterator. By following this guide, you’ll gain a clear understanding of how these components work together to manipulate dynamic data collections efficiently.

Using ArrayList and Iterator in Java

Creating an ArrayList

import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListExample {
    public static void main(String[] args) {
        // Creating an ArrayList of Strings
        ArrayList<String> fruits = new ArrayList<>();

        // Adding elements to the ArrayList
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");
        fruits.add("Date");

        // Displaying the ArrayList
        System.out.println("ArrayList: " + fruits);
    }
}

Explanation

  • Import Statements: Imports ArrayList and Iterator classes.
  • Creating an ArrayList: Initializes an ArrayList of String type.
  • Adding Elements: Uses the add() method to insert elements.
  • Displaying the ArrayList: Prints the elements.

Using an Iterator to Traverse the ArrayList

// Creating an Iterator
Iterator<String> iterator = fruits.iterator();

System.out.println("Iterating through the ArrayList:");
while (iterator.hasNext()) {
    String fruit = iterator.next();
    System.out.println(fruit);
}

Explanation

  • Creating an Iterator: Calls iterator() on the ArrayList.
  • Iterating Through Elements: Uses a while loop with hasNext() to check for remaining elements.
  • Accessing Elements: Retrieves elements with next().

Removing Elements Using Iterator

iterator = fruits.iterator(); // Re-initialize iterator

while (iterator.hasNext()) {
    String fruit = iterator.next();
    if (fruit.equals("Banana")) {
        iterator.remove(); // Removing 'Banana'
    }
}

System.out.println("ArrayList after removal: " + fruits);

Explanation

  • Re-initializing the Iterator: Resets the Iterator for a new traversal.
  • Removing Elements: Checks for “Banana” and removes it using remove().

Modifying Elements Using ListIterator

import java.util.ListIterator;

ListIterator<String> listIterator = fruits.listIterator();

while (listIterator.hasNext()) {
    String fruit = listIterator.next();
    if (fruit.equals("Cherry")) {
        listIterator.set("Citrus"); // Modifying 'Cherry' to 'Citrus'
    }
}

System.out.println("ArrayList after modification: " + fruits);

Explanation

  • Creating a ListIterator: Initializes a ListIterator for bi-directional traversal.
  • Modifying Elements: Uses set() to update “Cherry” to “Citrus”.

Complete Code Example

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

public class ArrayListExample {
    public static void main(String[] args) {
        // Creating an ArrayList of Strings
        ArrayList<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");
        fruits.add("Date");

        // Displaying the ArrayList
        System.out.println("ArrayList: " + fruits);

        // Using an Iterator to traverse the ArrayList
        Iterator<String> iterator = fruits.iterator();
        System.out.println("Iterating through the ArrayList:");
        while (iterator.hasNext()) {
            String fruit = iterator.next();
            System.out.println(fruit);
        }

        // Removing an element using Iterator
        iterator = fruits.iterator(); // Re-initialize iterator
        while (iterator.hasNext()) {
            String fruit = iterator.next();
            if (fruit.equals("Banana")) {
                iterator.remove(); // Removing 'Banana'
            }
        }
        System.out.println("ArrayList after removal: " + fruits);

        // Modifying elements using ListIterator
        ListIterator<String> listIterator = fruits.listIterator();
        while (listIterator.hasNext()) {
            String fruit = listIterator.next();
            if (fruit.equals("Cherry")) {
                listIterator.set("Citrus"); // Modifying 'Cherry' to 'Citrus'
            }
        }
        System.out.println("ArrayList after modification: " + fruits);
    }
}