Java中的 LongStream range()
LongStream range(long startInclusive, long endExclusive)以 1 的增量返回从 startInclusive(包含)到 endExclusive(不包含)的顺序有序 LongStream。
句法 :
static LongStream range(long startInclusive, long endExclusive)
参数 :
返回值:长元素范围的顺序 LongStream。
例子 :
// Implementation of LongStream range
// (long startInclusive, long endExclusive)
import java.util.*;
import java.util.stream.LongStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating an LongStream
LongStream stream = LongStream.range(6L, 10L);
// Displaying the elements in range
// including the lower bound but
// excluding the upper bound
stream.forEach(System.out::println);
}
}
输出:
6
7
8
9
注意: LongStream range(long startInclusive, long endExclusive) 基本上像 for 循环一样工作。可以按顺序生成等价的递增值序列:
for (int i = startInclusive; i < endExclusive ; i++)
{
...
...
...
}