foreach loops in Java

Source: Internet
Author: User

The foreach statement is one of the new features of Java5, and foreach provides developers with great convenience in traversing arrays and collections.
The foreach statement is a special simplified version of the for statement, but the foreach statement does not completely replace the For statement, however, any foreach statement can be rewritten as a version of the for statement.
foreach is not a keyword, and it is customary to call this special for-statement format a "foreach" statement. To understand the meaning of foreach as "for each" from the literal meaning of English. That's actually what this means.
Statement format for foreach:
for (element type T element variable x: Traverse object obj) {
A Java statement that references X;
}
A, foreach simplifies the traversal of arrays and collections
Here's a simple example of two examples to see how foreach simplifies programming. The code is as follows:

1 Importjava.util.Arrays; 2 Importjava.util.List; 3 Importjava.util.ArrayList; 4   5 /**  6 * Created by IntelliJ idea. 7 * User:leizhimin8 * date:2007-12-39 * time:16:58:24Ten * Summary of Use of foreach statements for new features of Java5 One */   A  Public classTestarray { -      Public Static voidMain (String args[]) { -Testarray test =NewTestarray ();  the Test.test1 ();  - Test.listtoarray ();  - test.testarray3 ();  -    +      }    -    +     /**   A * foreach statement output one-dimensional array at      */   -      Public voidtest1 () { -         //definition and initialization of an array -         intArr[] = {2, 3, 1};  -SYSTEM.OUT.PRINTLN ("----1----one-dimensional array before sorting");  -          for(intX:arr) {    inSYSTEM.OUT.PRINTLN (x);//output The value of an array element individually -          }    to    +         //sort an array - Arrays.sort (arr);  the    *         //using the new Java feature for Each loop output array $SYSTEM.OUT.PRINTLN ("----1----one-dimensional array after sorting"); Panax Notoginseng          for(intX:arr) {    -SYSTEM.OUT.PRINTLN (x);//output The value of an array element individually the          }    +      }    A    the     /**   + * Set conversion to one-dimensional arrays -      */   $      Public voidListtoarray () { $         //Create a list and add elements -list<string> list =NewArraylist<string>();  -List.add ("1");  theList.add ("3");  -List.add ("4"); Wuyi    the         //output A collection element using the Froeach statement -SYSTEM.OUT.PRINTLN ("----2----froeach Statement output collection element");  Wu          for(String x:list) { - System.out.println (x);  About          }    $    -         //convert ArrayList to arrays -Object s[] =List.toarray ();  -    A         //output A collection element using the Froeach statement +SYSTEM.OUT.PRINTLN ("----2----array elements converted from the Froeach statement output collection");  the          for(Object x:s) { -System.out.println (X.tostring ());//output The value of an array element individually $          }    the      }    the    the     /**   the * foreach output two-dimensional array test -      */   in      Public voidTestArray2 () { the         intArr2[][] = {{4, 3}, {1, 2}};  theSYSTEM.OUT.PRINTLN ("----3----foreach output two-dimensional array test");  About          for(intx[]: arr2) {    the              for(inte:x) { theSystem.out.println (e);//output The value of an array element individually the              }    +          }    -      }    the   Bayi     /**   the * foreach output three-dimensional array the      */   -      Public voidtestArray3 () { -         intArr[][][] = {    the{{1, 2}, {3, 4}},    the{{5, 6}, {7, 8}}    the          };  the    -SYSTEM.OUT.PRINTLN ("----4----foreach output three-dimensional array test");  the          for(int[] a2:arr) {    the              for(int[] a1:a2) {    the                  for(intx:a1) {   94 System.out.println (x);  the                  }    the              }    the          }   98      }    About}
View Code

Operation Result:


Ii. limitations of the foreach statement
The above example shows that if you want to reference an array or an index of a collection, the foreach statement cannot do it, and foreach simply faithfully passes through the group or the collection. Let's take a look at the following example:

foreach loops 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.