Java Arrays stream(long[] a) 方法



描述

Java Arrays stream(long[] a) 方法返回一個覆蓋指定陣列所有元素的 LongStream。

宣告

以下是 java.util.Arrays.stream(long[] a) 方法的宣告

public static LongStream stream​(long[] array)

引數

a − 這是陣列,假設在使用過程中不會被修改。

返回值

此方法返回陣列元素的流。

異常

Java Arrays stream​(long[] a, int fromIndex, int toIndex) 方法

描述

Java Arrays stream(long[] a, int fromIndex, int toIndex) 方法返回一個覆蓋指定陣列指定範圍的 LongStream。

宣告

以下是 java.util.Arrays.stream(long[] a, int fromIndex, int toIndex) 方法的宣告

public static LongStream stream​(long[] array, int fromIndex, int toIndex)

引數

  • a − 這是陣列,假設在使用過程中不會被修改。

  • fromIndex − 這是要覆蓋的第一個元素的索引,包含該元素。

  • toIndex − 這是要覆蓋的最後一個元素的索引,不包含該元素。

返回值

此方法返回陣列元素的流。

異常

  • ArrayIndexOutOfBoundsException − 如果 fromIndex < 0 或 toIndex > array.length

獲取長整型陣列的流示例

以下示例演示了 Java Arrays stream(long[]) 方法的用法。首先,我們建立了一個長整型陣列。使用 stream() 方法,之後列印陣列。

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
      // initialize array
      long arr[] = { 11L, 54L, 23L, 32L, 15L, 24L, 31L, 12L };

      System.out.print("Array: [ ");

      // use the stream to print each item
      Arrays.stream(arr).forEach(i -> System.out.print(i + " "));
      
      System.out.print("]");
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Array: [ 11 54 23 32 15 24 31 12 ]

使用範圍獲取長整型陣列的流示例

以下示例演示了 Java Arrays stream(long[], int, int) 方法的用法。首先,我們建立了一個長整型陣列。使用 stream() 方法,之後列印陣列。

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
      // initialize array
      long arr[] = { 11L, 54L, 23L, 32L, 15L, 24L, 31L, 12L };

      System.out.print("Array: [ ");

      // use the stream to print each item
      Arrays.stream(arr, 0, arr.length).forEach(i -> System.out.print(i + " "));
      
      System.out.print("]");
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Array: [ 11 54 23 32 15 24 31 12 ]

獲取長整型子陣列的流示例

以下示例演示了 Java Arrays stream(long[], int, int) 方法的用法。首先,我們建立了一個長整型陣列。使用 stream() 方法,之後列印一個子陣列。

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
      // initialize array
      long arr[] = { 11L, 54L, 23L, 32L, 15L, 24L, 31L, 12L };

      System.out.print("Array: [ ");

      // use the stream to print each item
      Arrays.stream(arr, 0, arr.length).forEach(i -> System.out.print(i + " "));
      
      System.out.print("]");
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Array: [ 11 54 23 32 15 ]
java_util_arrays.htm
廣告