public class IterateExample {
public static void main(String[] ar) {
// create list
List<String> strList = new ArrayList<String>();
// add value in list
strList.add("India");
strList.add("Nepal");
strList.add("China");
strList.add("Bhotan");
strList.add("Pakistan");
strList.add("ShriLanka");
// iterate via "for loop"
System.out.println("Simple for loop iterate ");
for (int i = 0; i < strList.size(); i++) {
System.out.println(strList.get(i));
}
// iterate via "New way to loop"
System.out.println(" Advance for loop iterate");
for (String temp : strList) {
System.out.println(temp);
}
// iterate via "iterator loop"
System.out.println("Iterator");
Iterator<String> itr = strList.iterator();
public static void main(String[] ar) {
// create list
List<String> strList = new ArrayList<String>();
// add value in list
strList.add("India");
strList.add("Nepal");
strList.add("China");
strList.add("Bhotan");
strList.add("Pakistan");
strList.add("ShriLanka");
// iterate via "for loop"
System.out.println("Simple for loop iterate ");
for (int i = 0; i < strList.size(); i++) {
System.out.println(strList.get(i));
}
// iterate via "New way to loop"
System.out.println(" Advance for loop iterate");
for (String temp : strList) {
System.out.println(temp);
}
// iterate via "iterator loop"
System.out.println("Iterator");
Iterator<String> itr = strList.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
// iterate via "while loop"
System.out.println(" While loop iterate ");
int i = 0;
while (i < strList.size()) {
System.out.println(strList.get(i));
i++;
}
}
}
// iterate via "while loop"
System.out.println(" While loop iterate ");
int i = 0;
while (i < strList.size()) {
System.out.println(strList.get(i));
i++;
}
}
}
Output :-
Simple for loop iterate
India
Nepal
China
Bhutan
Pakistan
ShriLanka
Advance for loop iterate
India
Nepal
China
Bhutan
Pakistan
ShriLanka
Iterator
India
Nepal
China
Bhutan
Pakistan
ShriLanka
While loop iterate
India
Nepal
China
Bhutan
Pakistan
ShriLanka
Thank you
No comments:
Post a Comment