在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;
}