java for range loop

http‮www//:s‬.lautturi.com
java for range loop

To create a for loop that iterates over a range of integers in Java, you can use the IntStream.range() method of the java.util.stream package. This method returns a stream of integers that can be iterated using the for-each loop.

Here's an example of how to use the IntStream.range() method to create a for loop that iterates over the integers from 0 to 9:

import java.util.stream.IntStream;

public class Main {
  public static void main(String[] args) {
    // Iterate over the integers from 0 to 9
    for (int i : IntStream.range(0, 10).toArray()) {
      System.out.println(i);
    }
  }
}

This code uses the IntStream.range() method to create a stream of integers from 0 to 9, and then uses the toArray() method to convert the stream to an array of integers. The for-each loop iterates over the items in the array and prints each integer to the console.

You can use the IntStream.range() method to create a for loop that iterates over any range of integers. Just pass the starting and ending values to the range() method, and the resulting stream will contain all the integers in the range.

Created Time:2017-11-03 15:57:14  Author:lautturi