For Each loop in Java

Source: Internet
Author: User

The 1.for each statement is one of the new features of Java 5 and provides a great convenience in iterating through collections and arrays.

The 2.for each statement is a special simplified version of the for statement, but the for every statement does not completely replace the For statement, however, any for each statement can be rewritten as a version of the for statement.

3.for Each statement format:

For (element data type element variable: Traverse object)

{

Loop body Content

}

Import Java.util.Arrays;   Import java.util.List;     Import java.util.ArrayList;            public class Testarray {public static void main (String args[]) {testarray test = new Testarray ();            Test.test1 ();            Test.listtoarray ();          Test.testarray3 ();  }/** * foreach statement output one-dimensional array */public void test1 () {//define and initialize an array int arr[] = {2,            3, 1};           SYSTEM.OUT.PRINTLN ("----1----one-dimensional array before sorting"); for (int x:arr) {System.out.println (x);//Output The value of the array element individually}//Sort the Arrays of the arrays.             Sort (arr);           Using the new Java feature for Each loop output array System.out.println ("----1----one-dimensional array after sorting");        for (int x:arr) {System.out.println (x);//Output The value of the array element individually}}/** * Collection converted to a one-dimensional array */public void Listtoarray () {//Create list and add element list<string> list = new Arraylist<s            Tring> (); List.add ("1");            List.add ("3");             List.add ("4");           Use the Froeach statement to output the collection element System.out.println ("----2----froeach statement output set element");            for (String x:list) {System.out.println (x);             }//Convert ArrayList to an array of Object s[] = List.toarray ();           Use the Froeach statement to output the collection element System.out.println ("----2----The Froeach statement output set of the array elements converted from the");       for (Object x:s) {System.out.println (x.tostring ());//Output The value of the array element individually}}/**            * foreach output two-dimensional array test */public void TestArray2 () {int arr2[][] = {{4, 3}, {1, 2}};           SYSTEM.OUT.PRINTLN ("----3----foreach output two-dimensional array test");                for (int x[]: arr2) {for (int e:x) {System.out.println (e);//output values of array elements individually }}}/** * foreach output three-dimensional array */public void TestArray3 () {int arr[][        [] = {{{1, 2}, {3, 4}},            {{5, 6}, {7, 8}}};           SYSTEM.OUT.PRINTLN ("----4----foreach output three-dimensional array test"); For (int[][] A2:arr) {in (int[] a1:a2) {for (int x:a1) {S                    YSTEM.OUT.PRINTLN (x); }                }            }        }   }

  

For Each loop in Java

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.