Home > Java > javaTutorial > body text

A brief discussion on java enhanced for loop for each

高洛峰
Release: 2017-01-21 16:47:21
Original
1145 people have browsed it

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, autoboxing, etc.).

The addition of For-Each loop simplifies collection traversal.

The syntax is as follows:

for(type element: array)
 
{
 
   System.out.println(element);
 
}
Copy after login

Example

You can directly look at the code for its basic use:

The code first compares the two A for loop; then implemented an enhanced for loop to traverse a two-dimensional array; and finally used three methods 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("----------旧方式遍历------------");
    //旧式方式   
    for(int i=0; i<arr.length; i++)
    {
      System.out.println(arr[i]);
    }
     
    System.out.println("---------新方式遍历-------------");
     
    //新式写法,增强的for循环
    for(int element:arr)
    {
      System.out.println(element);
    }
     
    System.out.println("---------遍历二维数组-------------");
     
    //遍历二维数组
     
    int[][] arr2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} ;
     
    for(int[] row : arr2)
    {
      for(int element : row)
      {
        System.out.println(element);
      }
    }
     
    //以三种方式遍历集合List
     
    List<String> list = new ArrayList<String>();
     
    list.add("a");
    list.add("b");
    list.add("c");
     
    System.out.println("----------方式1-----------");
    //第一种方式,普通for循环
    for(int i = 0; i < list.size(); i++)
    {
      System.out.println(list.get(i));
       
    }
     
    System.out.println("----------方式2-----------");
    //第二种方式,使用迭代器
    for(Iterator<String> iter = list.iterator(); iter.hasNext();)
    {
      System.out.println(iter.next());
    }
    System.out.println("----------方式3-----------");
    //第三种方式,使用增强型的for循环
    for(String str: list)
    {
      System.out.println(str);
       
    }
  }
 
}
Copy after login

Disadvantages of For-Each loop: index information is lost.

When traversing a collection or array, if you need to access the subscript of the collection or array, it is better to use the old-style way to implement the loop or traversal instead of using the enhanced for loop, because it loses the subscript information.

The above is the entire content of the java enhanced for loop for each brought to you by the editor. I hope it will be helpful to everyone. Please support the PHP Chinese website~

More More about Java's enhanced for loop for each related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!