What is for-each style loop?

All collection classes implement the Iterable interface, which means that a collection can be cycled through by use of for-each style loop.
Example:
import java.util.*;
class demo
{
public static void main(String[] args){
ArrayList al=new ArrayList();
al.add("abcd");
al.add("kjhh");
al.add("kkhf");
al.add("cdfbg");
al.add("actfd");
int i=1;
for (String x:al)
{

System.out.print("The "+i+++" element is:");
System.out.println(x);}
}};

No comments:

Post a Comment