如果您想将数组初始化为不同的值,您可以使用Arrays.fill() 方法。此方法将帮助您为数组的每个元素设置值。
import java.util.Arrays;
public class ArraysFillExample {
public static void main(String[] args) {
// Assign -1 to each elements of numbers array
int[] numbers = new int[5];
Arrays.fill(numbers, -1);
System.out.println("Numbers: " + Arrays.toString(numbers));
// Assign 1.0f to each elements of prices array
float[] prices = new float[5];
Arrays.fill(prices, 1.0f);
System.out.println("Prices : " + Arrays.toString(prices));
// Assign empty string to each elements of words array
String[] words = new String[5];
Arrays.fill(words, "");
System.out.println("Words : " + Arrays.toString(words));
// Assign 9 to each elements of the multi array
int[][] multi = new int[3][3];
for (int[] array : multi) {
Arrays.fill(array, 9);
}
System.out.println("Multi : " + Arrays.deepToString(multi));
}
}
上面代码sn-p的输出是:
Numbers: [-1, -1, -1, -1, -1]
Prices : [1.0, 1.0, 1.0, 1.0, 1.0]
Words : [, , , , ]
Multi : [[9, 9, 9], [9, 9, 9], [9, 9, 9]]
参考:https://kodejava.org/how-do-i-fill-array-with-non-default-value/