📜  Java中的 LongStream rangeClosed()

📅  最后修改于: 2022-05-13 01:55:18.630000             🧑  作者: Mango

Java中的 LongStream rangeClosed()

LongStream rangeClosed(long startInclusive, long endInclusive)以 1 的增量步长返回从 startInclusive(含)到 endInclusive(含)的 LongStream。

句法 :

static LongStream rangeClosed(long startInclusive, long endInclusive)

参数 :

  1. LongStream :原始长值元素的序列。
  2. startInclusive :包含的初始值。
  3. endInclusive :包含的上限。

返回值:长元素范围的顺序 LongStream。

例子 :

// Implementation of LongStream rangeClosed
// (long startInclusive, long endInclusive)
import java.util.*;
import java.util.stream.LongStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an LongStream
        LongStream stream = LongStream.rangeClosed(-4L, 3L);
  
        // Displaying the elements in range
        // including the lower and upper bound
        stream.forEach(System.out::println);
    }
}
输出:
-4
-3
-2
-1
0
1
2
3

注意: LongStream rangeClosed(long startInclusive, long endInclusive) 基本上像 for 循环一样工作。可以按顺序生成等价的递增值序列:

for (int i = startInclusive; i <= endInclusive ; i++) 
{
 ...
 ...
 ...
}