在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