Tag: array expression enhanced ACK code for loop array element equal array name
Java Enhanced for loop
JAVA5 introduces an enhanced for loop that is primarily used for arrays.
The Java enhanced for loop syntax format is as follows:
for( declaration statement: Expression ) { // code sentence }
declaration statement: declares a new local variable that must have a type that matches the type of the array element. Its scope is scoped to the Loop statement block, whose value is equal to the value of the array element at this time.
expression: The expression is the name of the array to access, or is the method that returns the value to the group.
Instance Test.java file code:Public Class Test { Public Static void Main(String Args[]){ Int [] Numbers={10,20,30,40,50};For(Int X:Numbers ){ System.Out.Print( X );System.Out.Print(",");} System.Out.Print("\ n");String [] Names={"James","Larry","Tom","Lacy"};For( String Name:Names ) { system.< Span class= "Hl-identifier" >out. Print ( name Span class= "Hl-brackets") system. Out. Print ( "," Span class= "Hl-brackets") } } }
The results of the above example compilation run as follows:
Ten, A,James,Larry,Tom,Lacy ,
Java Enhanced for loop