How to get ArrayList elements using Iterator with for loop?

If notice these lines of code this for-loop does not use an integer index.


								
package com.mindclues;

import java.util.*;

class IteratorTest {

    public static void main(String args[]) {
// create an array list 
        ArrayList list = new ArrayList();
// add elements to the array list 
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
        list.add("E");
        list.add("F");
        
        System.out.println("contents of list: ");

        for (Iterator listIter = list.iterator(); listIter.hasNext();) {
            System.out.println(listIter.next());
        }
    }
}
							

								
contents of list: 
A
B
C
D
E
F
							

Related Articles

post a comment