--Arrays ** allow nulls ** --The number of elements in the array is determined when it is initialized and cannot be changed later (elements ** cannot be added / deleted **. Overwriting is possible) --Since the array is a subclass of the object class, it can be regarded as ** object type **.
Example)
example(Object[] val); //Receives an array of object types as an argument
example(Object val) //Can accept an array as an argument
** [Points to note] **
--Is there a parenthesis [] when declaring a variable? → If there is no [] or if you use parentheses other than [] ** Compile error ** --Did you specify the number of elements when declaring the variable? → ** If you specified the number of elements ** when declaring the variable , ** Compile error ** - Is the number of elements specified ** when the instance is created **? -Is ** new described ** in ** instance generation **?
** *) It is possible to omit new and create an array with just the initializer ({})! !! ** ** --Automatically allocates memory areas for the number of elements specified by the initializer. --The initializer can be used only at the same time as the variable declaration. --Apart from variable declaration and initialization ** compilation error ** --The array initializer ** infers the array data type from the data type on the left side **
** [Example of array initialization] **
int a [][] = {{1,2},{3,4}}; //Generate an array with just an initializer without using new
int b [] = {}; //Generate an array with just the initializer without using new. The contents of the array are empty but not an error
int[][]c = new int [][]{}; //If you use both new and initializer[][]Must be empty
int[] d;
d = new int[]{2,3}; //After creating a variable to store the array, create an instance and assign it to the variable
int [] e = new int[3]; //Secure an instance area with 3 elements
** [Initialization example where a compile error occurs] **
int[] a;
a = int[2]; //New is not described at the time of instance creation, and the instance cannot be created.
int array = new int[2]; //Show array type[]There is no
int array[2]; //The number of elements is specified when the variable is declared.
int array1 = new int[2];
array1 = {1,2}; //The initializer can be used only at the same time as the variable declaration.
int[] array2= new int[3]{}; //When creating an anonymous array[]Do not describe the number of elements in
【point】
--Since the array is an instance, it cannot be used unless it is created.
--A variable that represents an array is a container for holding a ** reference ** to an array instance, and an array is not created in the variable. It is necessary to describe the number of elements to be handled in the array instance.
Recommended Posts