Java Arrays stream(double[] a) 方法



描述

Java Arrays stream(double[] a) 方法返回一個以指定陣列作為源的順序 DoubleStream。

宣告

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

public static DoubleStream stream​(double[] array)

引數

a - 這是陣列,假設在使用期間不會被修改。

返回值

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

異常

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

描述

Java Arrays stream(double[] a, int fromIndex, int toIndex) 方法返回一個以指定陣列的指定範圍作為源的順序 DoubleStream。

宣告

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

public static DoubleStream stream​(double[] array, int fromIndex, int toIndex)

引數

  • a - 這是陣列,假設在使用期間不會被修改。

  • fromIndex - 這是要包含的第一個元素的索引。

  • toIndex - 這是要排除的最後一個元素的索引

返回值

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

異常

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

獲取雙精度陣列流示例

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

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
      // initialize array
      double arr[] = { 11.0, 54.0, 23.0, 32.0, 15.0, 24.0, 31.0, 12.0 };

      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.0 54.0 23.0 32.0 15.0 24.0 31.0 12.0 ]

使用範圍獲取雙精度陣列流示例

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

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
      // initialize array
      double arr[] = { 11.0, 54.0, 23.0, 32.0, 15.0, 24.0, 31.0, 12.0 };

      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.0 54.0 23.0 32.0 15.0 24.0 31.0 12.0 ]

獲取雙精度子陣列流示例

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

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
      // initialize array
      double arr[] = { 11.0, 54.0, 23.0, 32.0, 15.0, 24.0, 31.0, 12.0 };

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

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

輸出

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

Array: [ 11.0 54.0 23.0 32.0 15.0 ]
java_util_arrays.htm
廣告