For-each Loop ~ Enhance for loop

Source: Internet
Author: User

Someone asked me what the enhanced for loop is, in fact I just use it, so I looked it up on the Internet, as follows: For-each cycle

The For-each loop is also called an enhanced for loop, or a foreach loop.

The For-each loop is a new feature of JDK5.0 (other new features such as generics, auto-boxing, etc.).

The addition of the For-each loop simplifies the traversal of the collection.

Its syntax is as follows:

For (type variable name: array)

{

SYSTEM.OUT.PRINTLN (variable name);

}

Example

Its basic use can be directly read the code:

The code first compares two for loops, then implements a two-dimensional array with an enhanced for loop, and finally iterates through a list collection in three ways.

  

Import Java.util.arraylist;import Java.util.iterator;import Java.util.list;public class foreachtest{public static Voi                D main (string[] args) {int[] arr = {1, 2, 3, 4, 5};        System.out.println ("----------old way Traversal------------");        Legacy mode for (int i=0; i<arr.length; i++) {System.out.println (arr[i]);                } System.out.println ("---------New way to traverse-------------");        New notation, enhanced for loop for (int element:arr) {System.out.println (element);                } System.out.println ("---------traverse two-dimensional array-------------");                Traverse the two-dimensional array int[][] arr2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};            For (int[] row:arr2) {for (int element:row) {System.out.println (element); }}//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 ("----------mode 1-----------");                    The first way, the normal for loop for (int i = 0; i < list.size (); i++) {System.out.println (List.get (i));        } System.out.println ("----------mode 2-----------");        The second way, use the iterator for (iterator<string> iter = List.iterator (); Iter.hasnext ();)        {System.out.println (Iter.next ());        } System.out.println ("----------mode 3-----------");                    The third way, use the enhanced for loop for (String str:list) {System.out.println (str); }    }}

Any new features that are advantageous will certainly have some drawbacks, and the enhanced for loop is also flawed in brevity:

1. When traversing a collection or array, if you need to access the index of a collection or array, it is best to use the old-fashioned way to loop or traverse, rather than using an enhanced for loop, because it loses the subscript information.

2. The effect of enhancing the For loop and iterator traversal is the same, which means that the internal enhancement for loop is called Iteratoer implementation, but there are some drawbacks to enhancing the For loop, such as the inability to dynamically delete the collection contents in the enhanced loop. Cannot get subscript and so on.

This article references from: http://www.cnblogs.com/mengdd/archive/2013/01/21/2870019.html

For-each Loop ~ Enhance for loop

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.