Java Arrays fill(short[], short) 方法



描述

Java Arrays fill(short[] a, short val) 方法將指定的 short 值賦給指定 short 陣列的每個元素。

宣告

以下是 java.util.Arrays.fill(short[] a, short val) 方法的宣告

public static void fill(short[] a, short val)

引數

  • a − 要填充的陣列。

  • val − 要儲存在陣列所有元素中的值。

返回值

此方法不返回值。

異常

Java Arrays fill(short[] a, int fromIndex, int toIndex, short val) 方法

描述

Java Arrays fill(short[] a, int fromIndex, int toIndex, short val) 方法將指定的 short 值賦給指定 short 陣列的指定範圍內的每個元素。要填充的範圍從索引 fromIndex(包含)到索引 toIndex(不包含)。(如果 fromIndex==toIndex,則要填充的範圍為空。)

宣告

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

public static void fill(short[] a, int fromIndex, int toIndex, short val)

引數

  • a − 要填充的陣列。

  • fromIndex − 這是第一個(包含)要填充指定值的元素的索引。

  • toIndex − 這是最後一個(不包含)要填充指定值的元素的索引。

  • val − 要儲存在陣列所有元素中的值。

返回值

此方法不返回值。

異常

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

  • IllegalArgumentException − 如果 fromIndex > toIndex

使用給定值填充 short 陣列示例

以下示例演示了 Java Arrays fill(short[], short) 方法的使用。首先,我們建立了一個 short 陣列並列印其元素。使用 fill(short[], short) 方法,我們用給定值填充陣列,然後再次列印更新後的陣列元素。

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {

      // initializing short array
      short arr[] = new short[] { 10 , 20, 15 };

      // let us print the values
      System.out.println("Actual values: ");
      for (short value : arr) {
         System.out.println("Value = " + value);
      }

      short replacement = 0;
      
      // using fill for placing value
      Arrays.fill(arr, replacement);

      // let us print the values
      System.out.println("New values after using fill() method: ");
      for (short value : arr) {
         System.out.println("Value = " + value);
      }
   }
}

輸出

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

Actual values: 
Value = 10
Value = 20
Value = 15
New values after using fill() method: 
Value = 0
Value = 0
Value = 0

使用給定值填充 short 子陣列示例

以下示例演示了 Java Arrays fill(short[], int, int, short) 方法的使用。首先,我們建立了一個 short 陣列並列印其元素。使用 fill(short[], int, int, short) 方法,我們用給定值填充陣列,然後再次列印更新後的陣列元素。

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {

      // initializing short array
      short arr[] = new short[] { 10, 20, 15 };

      // let us print the values
      System.out.println("Actual values: ");
      for (short value : arr) {
         System.out.println("Value = " + value);
      }

      short replacement = 0;
      
      // using fill for placing value from index 0 to 3
      Arrays.fill(arr, 0, 3, replacement);

      // let us print the values
      System.out.println("New values after using fill() method: ");
      for (short value : arr) {
         System.out.println("Value = " + value);
      }
   }
}

輸出

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

Actual values: 
Value = 10
Value = 20
Value = 15
New values after using fill() method: 
Value = 0
Value = 0
Value = 0

使用給定值填充 short 子陣列示例

以下示例演示了 Java Arrays fill(short[], int, int, short) 方法的使用。首先,我們建立了一個 short 陣列並列印其元素。使用 fill(short[], int, int, short) 方法,我們用給定值填充陣列的子陣列,然後再次列印更新後的陣列元素。

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {

      // initializing short array
      short arr[] = new short[] { 10, 20, 15 };

      // let us print the values
      System.out.println("Actual values: ");
      for (short value : arr) {
         System.out.println("Value = " + value);
      }

      short replacement = 0;
      
      // using fill for placing value from index 0 to 2
      Arrays.fill(arr, 0, 2, replacement);

      // let us print the values
      System.out.println("New values after using fill() method: ");
      for (short value : arr) {
         System.out.println("Value = " + value);
      }
   }
}

輸出

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

Actual values: 
Value = 10
Value = 20
Value = 15
New values after using fill() method: 
Value = 0
Value = 0
Value = 15
java_util_arrays.htm
廣告