Home Tutorial Java Collections Arraylist Java arraylist foreach

 
 

Java arraylist foreach
Posted on: October 23, 2009 at 12:00 AM
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


Related Tags for Java arraylist foreach:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.