[Java Silver] Array generation method

Array features

--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

How to generate an array

** [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

[Java Silver] Array generation method
[Java] New Thread generation method (2)
[Java Silver] About equals method
[Java] Random number generation method (Random)
[Java] New Thread generation method (1)
[Java] array
Java array
Java method
Java array
java (method)
java (array)
Java array
[Java] Array
Java array
Java method
[Java] method
java array
[Java] Array
[Java] method
java array variable
Java Silver exam procedure and learning method
Java Silver memo
How to use submit method (Java Silver)
[Java] Array notes
Java8 method reference
Java permutation generation
java8 method reference
[Java] split method
[Java SE 11 Silver] Arrays class method summary [Java beginner]
How to use the replace () method (Java Silver)
[Qualification Exam] Java SE 8 Silver Learning Method Summary
Passed Java SE8 Silver
JAVA DB connection method
java bronze silver passed
Java learning 2 (learning calculation method)
Java Silver Study Day 1
Java learning memo (method)
[Java] Stream API-Stream generation
Java Silver passing experience
About Java method binding
[Java ~ Method ~] Study memo (5)
About method splitting (Java)
Studying Java 8 (see method)
[Java Silver] About initialization
About inheritance (Java Silver)
Java programming (class method)
[Java ~ Array ~] Study memo 4
[Java] Password generation (Pasay)
Java8 Silver exam memorandum
[Java] List type / Array type conversion
[Experience] Passed Java SE 8 Silver
Java GC method determination conditions
JAVA Silver qualification exam record
Create a java method [Memo] [java11]
Java Silver exam preparation memo
Java test code method collection
Java SE8 Silver Pass Experience
[Java] Convert ArrayList to array
Benefits of Java static method
How to initialize Java array
[Beginner] Java basic "array" description