Java:for loop appears for (int i:arr)

Source: Internet
Author: User

When reading a book, notice that the for loop used by the author appears for (int I:arr), did not contact before, do not know its role, and then wrote a test of its role. It turns out that this is a traversal loop.

1  Public classTest {2      Public Static voidMain (string[] args) {3         int[] arr =New int[] {1, 2, 3};4          for(intI:arr) {5 System.out.println (i);6         }7     }8}

upon access to the information, For (:) {...} The JDK1.5 enhanced for loop is also called For-each. is to facilitate traversal of arrays and collection. Because collection implements the Iterable (iterative) interface, you can use For-each to traverse the container class.

Java:for loop appears for (int i:arr)

Related Article

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.