當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java LongStream rangeClosed()用法及代碼示例


LongStream rangeClosed(long startInclusive,long endInclusive)以增量為1返回從startInclusive(包括)到endInclusive(包括)的LongStream。

用法:

static LongStream rangeClosed(long startInclusive, long endInclusive)

參數:


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

返回值:用於long元素範圍的順序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++) 
{
 ...
 ...
 ...
}


相關用法


注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 LongStream rangeClosed() in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。