Java中的 IntStream rangeClosed()
IntStream rangeClosed(int startInclusive, int endInclusive)以 1 的增量步长返回一个从 startInclusive(含)到 endInclusive(含)的 IntStream。
句法 :
static IntStream rangeClosed(int startInclusive, int endInclusive)
参数 :
- IntStream :原始 int 值元素的序列。
- startInclusive :包含的初始值。
- endInclusive :包含的上限。
返回值: int 元素范围的顺序 IntStream。
例子 :
// Implementation of IntStream rangeClosed
// (int startInclusive, int endInclusive)
import java.util.*;
import java.util.stream.IntStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating an IntStream
IntStream stream = IntStream.rangeClosed(-4, 3);
// Displaying the elements in range
// including the lower and upper bound
stream.forEach(System.out::println);
}
}
输出:
-4
-3
-2
-1
0
1
2
3
注意: IntStream rangeClosed(int startInclusive, int endInclusive) 基本上像 for 循环一样工作。可以按顺序生成等价的递增值序列:
for (int i = startInclusive; i <= endInclusive ; i++)
{
...
...
...
}