Notice: Constant WP_MEMORY_LIMIT already defined in /home/javawith/public_html/codesjava.com/wp-config.php on line 107

Notice: Constant WP_MAX_MEMORY_LIMIT already defined in /home/javawith/public_html/codesjava.com/wp-config.php on line 108
Enhanced for loop in java - CodesJava

CodesJava

Easy learning with example program codes

Enhanced For Loop Java List Example


Notice: Undefined variable: footnotelist in /home/javawith/public_html/codesjava.com/wp-content/plugins/latex2html/inc/core.php on line 475

 
The enhanced for loop repeatedly executes a block of statements by iterating over a array or collection elements.

Syntax:

for(declaration : expression) {
   //Block of Statements
}

Where:
declaration: is used to declare the new variable.
expression: is the array or collection object to be iterated.

Program to use enhanced for loop example in java.

import java.util.ArrayList;
import java.util.List;
 
/**
 * Program to use enhanced for loop example in java.
 * @author CodesJava
 */
public class EnhancedForLoopExample {	
	static void enhancedForLoopTest(List<String> arrayList){
		//Enhanced For loop test
		for (String name : arrayList) {
			System.out.println(name);
		}
	}	
 
	public static void main(String args[]){ 
		//Create ArrayList object. 
		List<String> arrayList = new ArrayList<String>();   
		//Add objects to the HashSet. 
		arrayList.add("Mahesh"); 
		arrayList.add("Vishal"); 
		arrayList.add("Anil"); 
		arrayList.add("Binod"); 
		arrayList.add("Pardeep"); 
                arrayList.add("Neeraj"); 
		//method call 
		enhancedForLoopTest(arrayList); 
	}
}

Output:

Mahesh
Vishal
Anil
Binod
Pardeep
Neeraj

 

Please follow and like us:
error
Posted in Java   

Core Java Tutorial








Copyright © 2019 CodesJava DMCA.com Protection Status