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



描述

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

宣告

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

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

引數

  • a − 要填充的陣列。

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

返回值

此方法不返回值。

異常

  • ArrayStoreException − 如果指定的值不是可以儲存在指定陣列中的執行時型別。

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

描述

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

宣告

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

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

引數

  • a − 要填充的陣列。

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

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

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

返回值

此方法不返回值。

異常

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

  • IllegalArgumentException − 如果 fromIndex > toIndex

  • ArrayStoreException − 如果指定的值不是可以儲存在指定陣列中的執行時型別。

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

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

package com.tutorialspoint;

import java.util.Arrays;

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

      // initializing Student array
      Student arr[] = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") };

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

      Student replacement = new Student(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 (Student value : arr) {
         System.out.println("Value = " + value);
      }
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

輸出

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

Actual values: 
Value = [ 1, Julie ]
Value = [ 3, Adam ]
Value = [ 2, Robert ]
New values after using fill() method: 
Value = [ 0,  ]
Value = [ 0,  ]
Value = [ 0,  ]

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

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

package com.tutorialspoint;

import java.util.Arrays;

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

      // initializing Student array
      Student arr[] = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") };

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

      Student replacement = new Student(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 (Student value : arr) {
         System.out.println("Value = " + value);
      }
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

輸出

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

Actual values: 
Value = [ 1, Julie ]
Value = [ 3, Adam ]
Value = [ 2, Robert ]
New values after using fill() method: 
Value = [ 0,  ]
Value = [ 0,  ]
Value = [ 0,  ]

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

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

package com.tutorialspoint;

import java.util.Arrays;

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

      // initializing Student array
      Student arr[] = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") };

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

      Student replacement = new Student(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 (Student value : arr) {
         System.out.println("Value = " + value);
      }
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

輸出

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

Actual values: 
Value = [ 1, Julie ]
Value = [ 3, Adam ]
Value = [ 2, Robert ]
New values after using fill() method: 
Value = [ 0,  ]
Value = [ 0,  ]
Value = [ 2, Robert ]
java_util_arrays.htm
廣告