1st Method simple standard way to iterate.
2nd Method using for each loop.
/* IterateArrayList.java */
import java.util.ArrayList;
public class IterateArrayList
{
public static void main(String[] args)
{
ArrayList<Integer> data = new ArrayList<Integer>();
for (int i=0;i<10 ;i++ )
{
//Java 5 supports Autoboxing (int added in Arraylist
data.add(i);
}
System.out.println("Iterateing using 1st Method");
//Iterating ArrayList
for (int i=0;i<data.size() ;i++ )
{
System.out.println(data.get(i));
}
System.out.println("Iterateing using 2st Method");
//Iterating using for each loop
for(Integer val : data)
{
System.out.println(val);
}
}
}
/********OUTPUT********/
Iterateing using 1st Method
0
1
2
3
4
5
6
7
8
9
Iterateing using 2st Method
0
1
2
3
4
5
6
7
8
9