IntStream range(int startInclusive,int endExclusive)以1為增量步長從startInclusive(包括)到endExclusive(不包括)返回順序的有序IntStream。
用法:
static IntStream range(int startInclusive, int endExclusive)
參數:
IntStream :原始整數值元素的序列。
startInclusive :包含的初始值。
endExclusive :專屬上限。
返回值:一個int元素範圍的順序IntStream。
例:
// Implementation of IntStream range
// (int startInclusive, int endExclusive)
import java.util.*;
import java.util.stream.IntStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating an IntStream
IntStream stream = IntStream.range(6, 10);
// Displaying the elements in range
// including the lower bound but
// excluding the upper bound
stream.forEach(System.out::println);
}
}
輸出:
6
7
8
9
注意:IntStream範圍(int startInclusive,int endExclusive)本質上像for循環一樣工作。可以依次產生一個等效的遞增值序列,如下所示:
for (int i = startInclusive; i < endExclusive ; i++)
{
...
...
...
}