是的,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;
}