在Java中,可以通过以下几种方式初始化数组:
int[] arr = {1, 2, 3, 4, 5};
String[] strArr = {"Hello", "World"};
int[] arr = new int[5];
String[] strArr = new String[3];
int[] arr;
{
arr = new int[]{1, 2, 3, 4, 5};
}
int[] arr = new int[5];
for (int i = 0; i < arr.length; i++) {
arr[i] = i + 1;
}