Home

Example Application: Iterating an ArrayList Variable

 

Introduction

This is an example of iterating through an ArrayList variable:

package arraylist1;
import java.util.*;

public class Exercise {

    public static void main(String[] args) {
        ArrayList lstStudents = new ArrayList();
        
        lstStudents.add("Gertrude");
        lstStudents.add("James");
        lstStudents.add("Patricia");
        lstStudents.add("Hélène");
        lstStudents.add("Hank");

//        for(int i = 0; i < lstStudents.size(); i++)
  //          System.out.println("Student: " + lstStudents.get(i));

    //    System.out.println("");
        
//        for(Object str : lstStudents)
  //          System.out.println("Student: " + str);

        Iterator itr = lstStudents.iterator();

        while(itr.hasNext())
            System.out.println("Student: " + itr.next());
    }
}
 
 
 
 
 

Home Copyright © 2009 FunctionX, Inc.