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



描述

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

宣告

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

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

引數

  • a − 要填充的陣列。

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

返回值

此方法不返回值。

異常

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

描述

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

宣告

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

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

引數

  • a − 要填充的陣列。

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

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

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

返回值

此方法不返回值。

異常

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

  • IllegalArgumentException − 如果 fromIndex > toIndex

使用給定值填充布林型陣列示例

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

package com.tutorialspoint;

import java.util.Arrays;

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

      // initializing boolean array
      boolean arr[] = new boolean[] {true, true, false};

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

      // using fill for placing false
      Arrays.fill(arr, false);

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

輸出

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

Actual values:
Value = true
Value = true
Value = false
New values after using fill() method:
Value = false
Value = false
Value = false

使用給定值填充布林型子陣列示例

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

package com.tutorialspoint;

import java.util.Arrays;

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

      // initializing boolean array
      boolean arr[] = new boolean[] {true, true, true};

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

      // using fill for placing false from index 0 to 3
      Arrays.fill(arr, 0, 3, false);

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

輸出

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

Actual values: 
Value = true
Value = true
Value = true
New values after using fill() method: 
Value = false
Value = false
Value = false

使用給定值填充布林型子陣列示例

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

package com.tutorialspoint;

import java.util.Arrays;

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

      // initializing boolean array
      boolean arr[] = new boolean[] {true, true, true};

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

      // using fill for placing false from index 0 to 2
      Arrays.fill(arr, 0, 2, false);

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

輸出

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

Actual values: 
Value = true
Value = true
Value = true
New values after using fill() method: 
Value = false
Value = false
Value = true
java_util_arrays.htm
廣告

© . All rights reserved.