Java-util-arrays-fill-boolean-index
提供:Dev Guides
Java.util.Arrays.fill()メソッド
説明
- java.util.Arrays.fill(boolean [] a、int fromIndex、int toIndex、boolean val)*メソッドは、指定されたブール値の配列の指定された範囲の指定された範囲の各要素に指定されたブール値を割り当てます。インデックスfromIndexからインデックスtoIndex、エクスクルーシブに拡張します。 (fromIndex == toIndexの場合、入力される範囲は空です。)
宣言
以下は* java.util.Arrays.fill()*メソッドの宣言です
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.util.Arrays.fill()メソッドの使用法を示しています。
package com.finddevguides;
import java.util.Arrays;
public class ArrayDemo {
public static void main(String[] args) {
//initializing boolean array
boolean arr[] = new boolean[] {true, true, 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 1 to 4
Arrays.fill(arr, 1, 4, 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
Value = true
Value = true
New values after using fill() method:
Value = true
Value = false
Value = false
Value = false
Value = true