java.util.Arrays类的fill(int[] a, int val)方法将指定的int值赋值给指定int数组中的每个元素。
示例
import java.util.Arrays;
public class ArrayDemo {
public static void main(String[] args) {
int arr[] = new int[] {1, 6, 3, 2, 9};
System.out.println("Actual values: ");
for (int value : arr) {
System.out.println("Value = " + value);
}
Arrays.fill(arr, 18);
System.out.println("New values after using fill() method: ");
for (int value : arr) {
System.out.println("Value = " + value);
}
}
}
输出
Actual values:
Value = 1
Value = 6
Value = 3
Value = 2
Value = 9
New values after using fill() method:
Value = 18
Value = 18
Value = 18
Value = 18
Value = 18