package com.usr.collections;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IteratingArrayList {
public static void main(String[] args) {
List<String> list=new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");
list.add("four");
list.add("five");
System.out.println("Iterating with for loop:");
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
System.out.println("Iterating with for each loop:");
for(String list1:list){
System.out.println(list1);
}
System.out.println("using while loop:");
int j=0;
while(j<list.size()){
System.out.println(list.get(j));
j++;
}
System.out.println("using iterator:");
Iterator<String> itr=list.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IteratingArrayList {
public static void main(String[] args) {
List<String> list=new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");
list.add("four");
list.add("five");
System.out.println("Iterating with for loop:");
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
System.out.println("Iterating with for each loop:");
for(String list1:list){
System.out.println(list1);
}
System.out.println("using while loop:");
int j=0;
while(j<list.size()){
System.out.println(list.get(j));
j++;
}
System.out.println("using iterator:");
Iterator<String> itr=list.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.