Iterating Through Elements of an ArrayList - Java Collection Framework

Java examples for Collection Framework:ArrayList

Description

Iterating Through Elements of an ArrayList

Demo Code

import java.util.ArrayList;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    // Create an ArrayList of String
    List<String> nameList = new ArrayList<String>();

    // Add some names
    nameList.add("A");
    nameList.add("B");
    nameList.add("C");

    // Get the count of names in the list
    int count = nameList.size();

    // Let us print the name list
    System.out.println("List of names...");
    for (int i = 0; i < count; i++) {
      String name = nameList.get(i);
      System.out.println(name);/*from  w w w  .j  a  v a  2  s.  c  om*/
    }

    // Let us remove Kathleen from the list
    nameList.remove("Kathleen");

    // Get the count of names in the list again
    count = nameList.size();

    // Let us print the name list again
    for (int i = 0; i < count; i++) {
      String name = nameList.get(i);
      System.out.println(name);
    }
  }
}

Result


Related Tutorials