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



描述

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

宣告

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

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

引數

  • a − 這是要填充的陣列。

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

返回值

此方法不返回值。

異常

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

描述

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

宣告

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

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

引數

  • a − 這是要填充的陣列。

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

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

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

返回值

此方法不返回值。

異常

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

  • IllegalArgumentException − 如果 fromIndex > toIndex

使用給定值填充位元組陣列示例

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

package com.tutorialspoint;

import java.util.Arrays;

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

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

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

      byte 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 (byte 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

使用給定值填充位元組子陣列示例

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

package com.tutorialspoint;

import java.util.Arrays;

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

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

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

      byte 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 (byte 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

使用給定值填充位元組子陣列示例

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

package com.tutorialspoint;

import java.util.Arrays;

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

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

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

      byte 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 (byte 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
廣告