[] together.
int arr[];
int[] arr1;
int[][] arr2;
int arr3[][];
int[] arr4[];
int[][] arr5[];
arr = new int[2];
arr1 = new int[2];
arr2 = new int[2][1];
arr3 = new int[1][2];
arr4 = new int[2][];
arr5 = new int[2][][];
int arr[] = new int[2];
In the above, ʻarr [0]becomes0. Because the initial value of ʻint is 0.
0 elementsint array[] = new int[0];
0, the compilation will pass, but ʻarray [0]` does not exist. I get an exception when I try to access it.main method, the array with the number of elements 0 is set to ʻargs`.Use {...} if you want to initialize at the same time as creating an array instance.
{...} can only be used at the same time as the variable declaration.
int arr[] = {1, 2, 3};
If it is initialized, the number of elements cannot be specified.
int arr[] = new int[]{1, 2}; // OK
int arr[] = new int[2]{1, 2}; // NG
In the case of a multidimensional array, the number of dimensions of the variable and the number of dimensions of the reference destination must be the same.
int arr[][] = new int[][]{}; // OK
int arr[][] = new int[]{}; // NG
The following is also OK.
int arr1[][] = {};
TestBean testArr[] = new TestBean[2];
In the above, testArr [0] becomes null.
Declaring an array only creates an array instance, not each element of the array.
It needs to be initialized as follows.
testArr[0] = new TestBean();
Recommended Posts