這篇文章主要介紹“Java中Range函數怎么使用”,在日常操作中,相信很多人在Java中Range函數怎么使用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java中Range函數怎么使用”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
在Java中,Range方法在IntStream和LongStream類中都可用。在IntStream類中,它有助于返回函數參數范圍內IntStream的順序值。在該方法中,startInclusive(inclusive)
和endExclusive(exclusive)
是與增量步長一起使用的兩個參數,如前所述,將包括起始值,并排除結束值。在LongStream的情況下,唯一的區別是添加了LongStream值。
讓我們看看Java中range方法的語法。
static IntStream range(int startInclusive, int endExclusive)
參數:
IntStream:這是一個原始類型的int值元素序列。
startInclusive:包含在范圍中的初始值。
endExclusive:在范圍內排除的最后一個值或上限。
返回值:
該方法返回范圍中提到的int元素的連續int流作為參數。
static LongStream range(int startInclusive, int endExclusive)
參數:
LongStream:這是一個基元類型的長值元素序列。
startInclusive:包含在范圍中的初始值。
endExclusive:在范圍內排除的最后一個值或上限。
返回值:
該方法返回范圍中提到的長元素的連續長流作為參數。
首先,讓我們看看IntStream范圍在Java中是如何工作的。與Java中的其他類類似,這個類也需要一個必須首先導入的包。也就是說,為了使用IntStream類,導入包java.util.stream.IntStream
。導入后,創建一個IntStream,以便可以向其中添加元素。創建流后,使用方法range()
添加元素。在執行代碼時,將通過在參數中提到的范圍內的一個增量步驟返回一個序列有序IntStream。
要打印每個元素,請使用如下所示的方法。
intStream.forEach(System.out::println);
對于LongStream,首先導入包java.util.stream.LongStream
。與IntStream的功能類似,一旦導入包,就創建一個LongStream,以便可以向其中添加元素。創建流后,使用方法range()添加元素。在執行代碼時,將通過在參數中提到的范圍內的一個增量步驟返回序列有序的長流。
用于使用如下所示的方法打印每個元素。
LongStream.forEach(System.out::println);
在for循環的幫助下,可以按順序生成遞增元素的等效打印序列,
如下所示:
for (inti = startInclusive; i<endExclusive ; i++) {... . . . }
以下是提到的示例:
示例#1
Java程序實現IntStream Range函數。
代碼:
// 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); } }
輸出:
首先,導入包java.util.stream.IntStream
。然后,創建一個IntStream st
,用于向其中添加元素。在創建流的過程中,使用方法range(32,45)添加元素,其中包括32個元素,排除45個元素。在執行代碼時,將通過一個增量步驟從32到44返回一個有序的IntStream,如示例輸出中所示。
示例#2
Java程序實現LongStream range范圍函數。
代碼:
// 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); } }
輸出:
與上述程序類似,導入包java.util.stream.LongStream
。然后,創建一個具有方法range(100001L、100010L)
的LongStreamst,用于向其添加元素。在執行代碼時,將通過一個增量步驟從100001L返回到100010L,如示例輸出所示。
示例#3
Java程序,用于組合實現LongStream和IntStream range范圍函數。
代碼:
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); } }
輸出:
導入包java.util.stream.IntStream
和 java.util.stream.LongStream
。然后,創建IntStreamstr和LongStreamst以向其中添加元素。在創建流期間,使用方法range(32,45)
在IntStream中添加元素,其中包括32,排除45。同樣,使用方法range(100001L、100010L)
在LongStream中添加元素。在執行代碼時,序列有序IntStream將從32返回到44,LongStream將通過增量步驟1從100001L返回到100010L。
到此,關于“Java中Range函數怎么使用”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。