阅读量:59
在Java中,可以使用以下几种方式对数组进行初始化:
- 使用数组字面量进行初始化:
int[] numbers = {1, 2, 3, 4, 5};
String[] names = {"Alice", "Bob", "Charlie"};
- 使用new关键字创建数组并指定初始化大小:
int[] numbers = new int[5];
String[] names = new String[3];
- 使用循环对数组进行初始化:
int[] numbers = new int[5];
for (int i = 0; i < numbers xss=removed class="hljs-number">1;
}
- 使用Arrays类的静态方法对数组进行初始化:
int[] numbers = new int[5];
Arrays.fill(numbers, 0); // 将数组所有元素填充为0