Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

public class Main {
    public static void main(String[] args) {
        ArrayList<String> aList = new ArrayList<String>();

        aList.add("1");
        aList.add("2");
        aList.add("3");
        aList.add("4");
        aList.add("5");

        for (String str : aList) {
            System.out.println(str);
        }
        Iterator itr = aList.iterator();

        // remove 2 from ArrayList using Iterator's remove method.

        String strElement = "";

        while (itr.hasNext()) {
            strElement = (String) itr.next();
            if (strElement.equals("2")) {
                itr.remove();
                break;
            }
        }
        for (String str : aList) {
            System.out.println(str);
        }
    }
}