Repeat the same process a specified number of times
for (int i = 0 ; i < 10; i++ ) {
System.out.print(i);
}
Execution result ➡ 0123456789
for (initialization; conditional expression; change expression) Repeat the processing in the block while the conditional expression is true
Repeat as long as there are array or List elements Unlike the for statement, there are no conditional expressions or change expressions
Integer[] array = {0,1,2,3,4,5,6,7,8,9};
for (Integer str: array) {
System.out.print(str);
}
Execution result ➡ 0123456789
for (Variable declaration: Array to retrieve / List name) Repeat the process in the block as long as there are elements
IntStream I received the advice that using IntStream is easy. Thank you for your advice!
IntStream.range returns a continuous value (IntStream) from the specified start value to the end value -1.
ʻIntStream.range (start value, end value) `
The processing performed by the for statement is as follows when using IntStream.
import java.util.stream.IntStream;
public static void main(String[] args) {
IntStream.range(0, 10).forEach(System.out::print);
}
It's nice because you don't have to write conditional expressions for initialization and increments.
Recommended Posts