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 |
You are the BEST!!!!
ReplyDelete