java range()_Java IntStream range()用法及代码示例

IntStream range(int startInclusive,int endExclusive)以1为增量步长从startInclusive(包括)到endExclusive(不包括)返回顺序的有序IntStream。

用法:

static IntStream range(int startInclusive, int endExclusive)

参数:

IntStream :原始整数值元素的序列。

startInclusive :包含的初始值。

endExclusive :专属上限。

返回值:一个int元素范围的顺序IntStream。

例:

// 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范围(int startInclusive,int endExclusive)本质上像for循环一样工作。可以依次产生一个等效的递增值序列,如下所示:

for (int i = startInclusive; i < endExclusive ; i++)

{

...

...

...

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
IntStream.range()是Java中的一个方法,用于创建一个顺序的、从指定的起始值到指定的结束值(不包括结束值)的整数流。在使用IntStream.range()之前,需要先导入java.util.stream.IntStream包。举个例子,如果我们想创建一个从1到5的整数流,可以使用IntStream.range(1, 5)。然后我们可以通过调用forEach()方法来遍历并打印出整数流中的元素。这样的话,我们将会打印出1, 2, 3, 4这四个数字。下面是一个示例代码: import java.util.stream.IntStream; class RangeTest { public static void main(String[] args) { IntStream intStream = IntStream.range(1, 5); intStream.forEach(System.out::println); } } 运行结果如下: 1 2 3 4 所以,IntStream.range()是一个很方便的方法,可以用来生成一连串的整数序列,并在后续的操作中对这些整数进行处理。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [Java中的IntStream range()方法的使用](https://blog.csdn.net/u012660464/article/details/116529342)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [Java Stream.of()和IntStream.range()用法及区别](https://blog.csdn.net/m0_60105488/article/details/118728600)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值