Java-foreach statement usage Summary

Source: Internet
Author: User

Foreach statements Use Summary foreach statements are one of the new features of Java 5. In terms of traversing arrays and collections, foreach provides great convenience for developers. The foreach statement is a special simplified version of the for statement, but the foreach statement cannot completely replace the for statement. However, any foreach statement can be changed to the for statement version. Foreach is not a keyword. It is used to calling this special for statement format "foreach. Meaning foreach, that is,"
The meaning of each. This is actually what it means. Foreach statement format: For (element type T element variable X: traversing object OBJ) {references the Java Statement of X ;} the following two examples show how foreach simplifies programming. The Code is as follows: 1. foreach simplifies array and set traversal import java. util. arrays;
Import java. util. List;
Import java. util. arraylist;

/**
* Created by intellij idea.
* User: leizhimin
* Date: 2007-12-3
* Time: 16:58:24
* Java 5 new features: foreach statement usage Summary
*/
Public class testarray {
Public static void main (string ARGs []) {
Testarray test = new testarray ();
Test. test1 ();
Test. listtoarray ();
Test. testarray3 ();

}

/**
* The foreach statement outputs a 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); // outputs the values of array elements one by one.
}

// Sort the Array
Arrays. Sort (ARR );

// Use the new Java feature for each to output arrays cyclically
System. Out. println ("---- 1 ---- sorted one-dimensional array ");
For (int x: ARR ){
System. Out. println (x); // outputs the values of array elements one by one.
}
}

/**
* Converting a set to a one-dimensional array
*/
Public void listtoarray (){
// Create a list and add elements
List <string> List = new arraylist <string> ();
List. Add ("1 ");
List. Add ("3 ");
List. Add ("4 ");

// Use the froeach statement to output the set Element
System. Out. println ("---- 2 ---- froeach statement output set element ");
For (string X: List ){
System. Out. println (X );
}

// Convert arraylist to an array
Object s [] = List. toarray ();

// Use the froeach statement to output the set Element
System. Out. println ("---- 2 ---- froeach statement output the array element converted from the set ");
For (Object X: S ){
System. Out. println (X. tostring (); // outputs the values of array elements one by one.
}
}

/**
* Foreach outputs a two-dimensional array Test
*/
Public void testarray2 (){
Int arr2 [] [] = {4, 3}, {1, 2 }};
System. Out. println ("---- 3 ---- foreach outputs two-dimensional array test ");
For (int x []: arr2 ){
For (int e: X ){
System. Out. println (E); // outputs the values of array elements one by one.
}
}
}

/**
* Foreach outputs a three-dimensional array.
*/
Public void testarray3 (){
Int arr [] [] [] = {
{1, 2}, {3, 4 }},
{5, 6}, {7, 8 }}
};

System. Out. println ("---- 4 ---- foreach output 3D array test ");
For (INT [] [] A2: ARR ){
For (INT [] A1: A2 ){
For (int x: A1 ){
System. Out. println (X );
}
}
}
}
} Running result: ---- 1 ---- one-dimensional array before sorting
2
3
1
---- 1 ---- sorted one-dimensional array
1
2
3
---- 2 ---- froeach statement output set Element
1
3
4
---- 2 ---- froeach statement output the array elements converted from the set
1
3
4
---- 4 ---- foreach output 3D array Test
1
2
3
4
5
6
7
8

Process finished with exit code 0 II. Limitations of the foreach statement the preceding example shows that if you want to reference the index of an array or set, the foreach statement cannot do so, foreach just honestly traverses the array or combines the array. The following is an example to understand :/**
* Created by intellij idea.
* User: leizhimin
* Date: 2007-12-3
* Time: 17:07:30
* Limitations of foreach statements
*/
Public class testarray2 {

Public static void main (string ARGs []) {
// Define a one-dimensional array
Int arr [] = new int [4];
System. Out. println ("---- output the defined array before the value is assigned ----");
For (int x: ARR ){
System. Out. println (X );
}

// Assign values to array elements through Indexes
System. Out. println ("---- assigning values to array elements through cyclic variables ----");
For (INT I = 3; I> 0; I --){
Arr [I] = I;
}
// Cyclically output the created Array
System. Out. println ("---- after the value is assigned, foreach outputs the created array ----");
For (int x: ARR ){
System. Out. println (X );
}
}
}
Running result: ---- output the defined array before the value is assigned ----
0
0
0
0
---- Assign values to array elements through cyclic variables ----
---- After the value is assigned, foreach outputs the created array ----
0
1
2
3

Process finished with exit code 0

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.