Share on Google+Share on Google+

Java arraylist foreach

Advertisement
This section demonstrates the use of the foreach loop in the arraylist

  • In the arraylist, foreach loop is easily used.
  • It takes element one by one from the array and put it into the variable of the loop


Example Java arraylist foreach
import java.util.ArrayList;
import java.util.List;

public class list_for_each {
    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++) {
        list.add(names[i]);
        }
        for (String a:names) {
        System.out.println(a);
        }
    }
}
Output
mac
john
alexender
rock
alex


Advertisements

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.