range()
Java中IntStream类中的方法用于以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);
以下是range()
在Java中实现IntStream方法的示例-
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