Intro
This examples, will demonstrates how to use Enhanced For Loop.
Examples
package com.freesamplecode.java.basic;
import java.util.ArrayList;
import java.util.List;
public class EnhancedForLoopDemo {
public static void main(String[] args){
String[] fruitsArray = new String[]{"Apple","Banana","Cherry","Pinneapple","Strawberry"};
//enhanced for loop an array of string
System.out.println("My favorite fruits are : ");
for(String fruit : fruitsArray){
System.out.println(fruit);
}
System.out.println("\n");
List<String> listOfEmployee = new ArrayList<String>();
listOfEmployee.add("Nursalim");
listOfEmployee.add("Nurul Hikmah");
listOfEmployee.add("Iskiyati");
//enhanced for loop a list
System.out.println("List of employees : ");
for(String employee : listOfEmployee){
System.out.println(employee);
}
}
}
Output
My favorite fruits are : Apple Banana Cherry Pinneapple Strawberry List of employees : Nursalim Nurul Hikmah Iskiyati

0 comments: