Share on Google+Share on Google+

Java arraylist for loop

This section demonstrates the use of for loop in the ArrayList in Java.

  • Elements of the ArrayList can be easily added and retrieved by the for loop.

Example FOR Java arraylist for loop
import java.util.ArrayList;
import java.util.List;

public class List_for {
    public static void main(String[] args) {
        List list=new ArrayList();
        String names[]={"mac","john","alexender","rock","alex"};
        for (int i = 0; i < names.length; i++) {
        for (int i = 0; i < names.length; i++) {
        System.out.println((i+1)+" : "+list.get(i));
1 : mac
2 : john
3 : alexender
4 : rock
5 : alex


Posted on: October 23, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.