Wednesday, October 2, 2013

Access List elements with Iterator and short form of for-loop

Example to access List elements with Iterator and short form of for-loop
package java_list;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class Java_List {

    public static void main(String[] args) {

        //Convert from Array of String to List of String
        List<String> list = 
                Arrays.asList("One", "Two", "Three", "Four", "Five");
        
        System.out.println("Access List using Iterator");
        Iterator iterator = list.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
        System.out.println();
        System.out.println("Access List using Short form of for-loop");
        for(String s: list){
            System.out.println(s);
        }
    }
}


Access List elements with Iterator and short form of for-loop
Access List elements with Iterator and short form of for-loop


1 comment: