Java中的IntStream range()

IntStream范围(int startInclusive,int endExclusive) 以增量步骤1返回从startInclusive(包含)到endExclusive(独占)的顺序IntStream。

null

语法:

static IntStream range(int startInclusive,   int endExclusive)

参数:

  • IntStream: 一个原始的int值元素序列。
  • 开始结论: 包含的初始值。
  • 特有的: 唯一的上限。

    返回值: 整数元素范围的连续整数流。

    例子:

    // 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范围(intstartinclusive,intendexclusive)基本上就像for循环一样工作。可以按如下顺序生成一个等效的递增序列:

    for (int i = startInclusive; i < endExclusive ; i++) 
    {
     ...
     ...
     ...
    }
    
  • © 版权声明
    THE END
    喜欢就支持一下吧
    点赞13 分享