I wasn't used to using extended for statements to implement iterative processing in Java, so I decided to review the simple usage this time. It is a level that I hope that beginner programmers will find it useful in terms of content.
Please note that the extended for statement is a function of Java SE 5.0 or later, so it is not possible if you are using Java earlier than that.
--You can take out the elements of an array or collection one by one and execute processing on each element. --The description can be simplified
First of all, how to use the conventional for statement is as follows
Traditional notation
for(Equation 1;Equation 2;Equation 3){
processing;
}
And the extended for statement is as follows
Extended for statement
for(Variable declaration:Reference variable name){ //In other words, in parentheses(Type variable name:formula)
processing;
}
Immediately write the process
sample.java
public static void main (String args[]) {
//Finally put the elements in the array
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Gorilla");
list.add("Rappa");
list.add("Pineapple");
list.add("rumba");
//From here is the production
//Extracts all elements of the variable name list one by one and outputs them as standard output.
for(String word : list) { // for(Type variable name:formula)
System.out.println(word);
}
}
The output result is as follows
result
Apple
Gorilla
Rappa
Pineapple
rumba
――This example is simple, but if you can master it, it will be easier to describe the iterative process!
Recommended Posts