IntStream range() 方法在 Java 中


IntStream 类中的 range() 方法用于以 1 的增量步长返回从 startInclusive 到 endExclusive 的序列有序 IntStream。这也包括 startInclusive。

其语法如下 −

static IntStream range(int startInclusive, int endExclusive)

其中,参数 startInclusive 包含起始值,而 endExclusive 则排除最后的值

如需使用 Java 中的 IntStream 类,请导入以下包 −

import java.util.stream.IntStream;

创建 IntStream 并使用 range() 方法在范围内添加流元素。这会返回一个在该范围内以 1 的增量步长序列有序的 IntStream −

intStream.forEach(System.out::println);

以下是在 Java 中实现 IntStream range() 方法的一个示例 −

示例

 实时演示

import java.util.*;
import java.util.stream.IntStream;

public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.range(20, 30);
      intStream.forEach(System.out::println);
   }
}

输出

20
21
22
23
24
25
26
27
28
29

更新时间:30-Jul-2019

9K+ 浏览

开启你的 职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.