This section demonstrates the use of the foreach loop in the arraylist
This section demonstrates the use of the foreach loop in the arraylist
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