For-Each Loop
The For-Each loop is also called an enhanced for loop, or a foreach loop.
For-Each loop is a new feature of JDK5.0 (other new features such as generics, automatic boxing, etc.).
The addition of the For-Each loop simplifies the traversal of the collection.
The syntax is as follows:
for(type element: array){ System.out.println(element);}example
For basic usage, you can directly look at the code:
The code first compares two for loops; then implements the use of enhanced for loops to traverse a two-dimensional array; finally, three methods are used to traverse a List collection.
import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class ForeachTest{ public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- System.out.println("-----------------------------------"); //New writing, enhanced for loop for(int element:arr) { System.out.println(element); } System.out.println("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //Iterate through the collection in three ways List List<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); list.add("c"); System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- list.iterator(); iter.hasNext();) { System.out.println(iter.next()); } System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Disadvantages of For-Each loop: The index information is lost.
When traversing a collection or array, if you need to access the subscript of the collection or array, it is best to use the old-style way to implement loops or traversals instead of using an enhanced for loop, because it loses the subscript information.
The above is the full content of the brief discussion of Java enhanced for loop for each brought to you. I hope it will be helpful to everyone and support Wulin.com more~