欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中Range函數(shù)的簡單介紹

 更新時間:2022年07月21日 15:58:11   作者:?sofia?  
這篇文章主要介紹了Java中Range函數(shù)的簡單介紹,Java中的range方法用于返回IntStream和LongStream在函數(shù)參數(shù)范圍內(nèi)的順序值

前言

在Java中,Range方法在IntStream和LongStream類中都可用。在IntStream類中,它有助于返回函數(shù)參數(shù)范圍內(nèi)IntStream的順序值。在該方法中,startInclusive(inclusive)endExclusive(exclusive)是與增量步長一起使用的兩個參數(shù),如前所述,將包括起始值,并排除結(jié)束值。在LongStream的情況下,唯一的區(qū)別是添加了LongStream值。

Range語法

讓我們看看Java中range方法的語法。

IntStream范圍的語法

static IntStream range(int startInclusive,   int endExclusive)

參數(shù):

  • IntStream:這是一個原始類型的int值元素序列。
  • startInclusive:包含在范圍中的初始值。
  • endExclusive:在范圍內(nèi)排除的最后一個值或上限。

返回值:

該方法返回范圍中提到的int元素的連續(xù)int流作為參數(shù)。

LongStream范圍的語法

static LongStream range(int startInclusive,   int endExclusive)

參數(shù):

  • LongStream:這是一個基元類型的長值元素序列。
  • startInclusive:包含在范圍中的初始值。
  • endExclusive:在范圍內(nèi)排除的最后一個值或上限。

返回值:

該方法返回范圍中提到的長元素的連續(xù)長流作為參數(shù)。

Range函數(shù)在Java中是如何工作的?

首先,讓我們看看IntStream范圍在Java中是如何工作的。與Java中的其他類類似,這個類也需要一個必須首先導(dǎo)入的包。也就是說,為了使用IntStream類,導(dǎo)入包java.util.stream.IntStream。導(dǎo)入后,創(chuàng)建一個IntStream,以便可以向其中添加元素。創(chuàng)建流后,使用方法range()添加元素。在執(zhí)行代碼時,將通過在參數(shù)中提到的范圍內(nèi)的一個增量步驟返回一個序列有序IntStream。

要打印每個元素,請使用如下所示的方法。

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

對于LongStream,首先導(dǎo)入包java.util.stream.LongStream。與IntStream的功能類似,一旦導(dǎo)入包,就創(chuàng)建一個LongStream,以便可以向其中添加元素。創(chuàng)建流后,使用方法range()添加元素。在執(zhí)行代碼時,將通過在參數(shù)中提到的范圍內(nèi)的一個增量步驟返回序列有序的長流。

用于使用如下所示的方法打印每個元素。

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

在for循環(huán)的幫助下,可以按順序生成遞增元素的等效打印序列,

如下所示:

for (inti = startInclusive; i<endExclusive ; i++)
{... . . . }

Java中的Range示例

以下是提到的示例:

示例#1

Java程序?qū)崿F(xiàn)IntStream Range函數(shù)。

代碼:

// IntStream range implementation using Java
import java.util.*;
//import the package for IntStream
import java.util.stream.IntStream;
public class RangeExample {
// main method
public static void main(String[] args)
{
// Create an IntStream
IntStream st = IntStream.range(32, 45);
// Display the elements in the range mentioned as 32 and 45 where 32 is included and 45 is excluded
System.out.println("The elements are:");
st.forEach(System.out::println);
} }

輸出:

首先,導(dǎo)入包java.util.stream.IntStream。然后,創(chuàng)建一個IntStream st,用于向其中添加元素。在創(chuàng)建流的過程中,使用方法range(32,45)添加元素,其中包括32個元素,排除45個元素。在執(zhí)行代碼時,將通過一個增量步驟從32到44返回一個有序的IntStream,如示例輸出中所示。

示例#2

Java程序?qū)崿F(xiàn)LongStream range范圍函數(shù)。

代碼:

// LongStream range implementation using Java
import java.util.*;
//import the package for LongStream
import java.util.stream.LongStream;
public class RangeExample {
// main method
public static void main(String[] args)
{
// Create a LongStream
LongStream st = LongStream.range(1000001L, 1000010L);
// Display the elements in the range mentioned as 1000001L and 1000010L where 1000001L is included and 1000010L is excluded
System.out.println("The elements are:");
st.forEach(System.out::println);
} }

輸出:

與上述程序類似,導(dǎo)入包java.util.stream.LongStream。然后,創(chuàng)建一個具有方法range(100001L、100010L)的LongStreamst,用于向其添加元素。在執(zhí)行代碼時,將通過一個增量步驟從100001L返回到100010L,如示例輸出所示。

示例#3

Java程序,用于組合實現(xiàn)LongStream和IntStream range范圍函數(shù)。

代碼:

import java.util.*;
//import the package for IntStream
import java.util.stream.IntStream;
//import the package for LongStream
import java.util.stream.LongStream;
public class RangeExample {
// main method
public static void main(String[] args)
{
// Create an IntStream
IntStream str = IntStream.range(32, 45);
// Display the elements in the range mentioned as 32 and 45 where 32 is included and 45 is excluded
System.out.println("The IntStream elements are:");
str.forEach(System.out::println);
// Create a LongStream
LongStream st = LongStream.range(1000001L, 1000010L);
// Display the elements in the range mentioned as 1000001L and 1000010L where 1000001L is included and 1000010L is excluded
System.out.println("The LongStream elements are:");
st.forEach(System.out::println);
} }

輸出:

導(dǎo)入包java.util.stream.IntStream 和 java.util.stream.LongStream。然后,創(chuàng)建IntStreamstr和LongStreamst以向其中添加元素。在創(chuàng)建流期間,使用方法range(32,45)在IntStream中添加元素,其中包括32,排除45。同樣,使用方法range(100001L、100010L)在LongStream中添加元素。在執(zhí)行代碼時,序列有序IntStream將從32返回到44,LongStream將通過增量步驟1從100001L返回到100010L。

到此這篇關(guān)于Java中Range函數(shù)的簡單介紹的文章就介紹到這了,更多相關(guān)Java Range函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論