在Java中,可以使用以下几种方式对数组进行初始化:
int[] numbers = {1, 2, 3, 4, 5};
String[] names = {"Alice", "Bob", "Charlie"};
int[] numbers = new int[5];
String[] names = new String[3];
int[] numbers = new int[5];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = i + 1;
}
int[] numbers = new int[5];
Arrays.fill(numbers, 0); // 将数组所有元素填充为0