How to get ArrayList elements using Iterator with while loop?

In this program we are get list element using simple Iterator.There are two ways of using Iterator,here we are using while loop for fetch ArrayList elements.


								
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");
// use iterator to display contents of list 
        System.out.println("contents of list: ");
        Iterator itr = list.iterator();
        while (itr.hasNext()) {
            Object element = itr.next();
            System.out.println(element + " ");

        }
    }
}
							

								
contents of list: 
A 
B 
C 
D 
E 
F
							

Related Articles

post a comment