是的,Java中的數組初始化是可行的。在Java中,您可以在聲明數組時直接初始化數組的元素。以下是一些示例:
int[] numbers = {1, 2, 3, 4, 5};
String[] names = {"Alice", "Bob", "Charlie"};
Object[] mixedArray = {"Hello", 42, 3.14};
您還可以在聲明數組時指定數組的大小,然后將元素逐個添加到數組中。例如:
int[] numbers = new int[5];
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
numbers[4] = 5;
或者使用循環來簡化初始化過程:
int[] numbers = new int[5];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = i + 1;
}