Organized memo in the head (Java --Array)

My Java environment

Array

Array declaration

Declaration example


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][][];

Array instance generation

For primitive types


int arr[] = new int[2];

In the above, ʻarr [0]becomes0. Because the initial value of ʻint is 0.

Array with 0 elements
int array[] = new int[0];
Array initialization

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[][] = {};

For reference type


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

Organized memo in the head (Java --Array)
Organized memo in the head (Java --Control syntax)
Organized memo in the head (Java --instance edition)
Organized memo in the head (Java --Data type)
Organized notes in the head (Java-Exceptions)
[Java ~ Array ~] Study memo 4
[Java] ArrayList → Should I specify the size in array conversion?
Access the network interface in Java
Guess the character code in Java
Java learning memo (creating an array)
Unzip the zip file in Java
Parsing the COTOHA API in Java
Call the super method in Java
Java array
Java array
java (array)
Java array
[Java] Array
Java memo
Java array
java array
[Java] Array
When seeking multiple in a Java array
Get the result of POST in Java
Java reference to understand in the figure
Try using the Stream API in Java
Call the Windows Notification API in Java
Map without using an array in java
Memo: [Java] Check the contents of the directory
I tried the new era in Java
[Java] Use cryptography in the standard library
Compare the elements of an array (Java)
Try calling the CORBA service in Java 11+
What is the main method in Java?
How to get the date in java
The story of writing Java in Emacs
Console input in Java (understanding the mechanism)
Display message dialog in java (personal memo)
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
java anything memo
Regarding the transient modifier and serialization in Java
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
About the confusion seen in startup Java servers
java array variable
The story of making ordinary Othello in Java
About the idea of anonymous classes in Java
Java Silver memo
ChatWork4j for using the ChatWork API in Java
A story about the JDK in the Java 11 era
Partization in Java
java, maven memo
The intersection type introduced in Java 10 is amazing (?)
The story of learning Java in the first programming
Measure the size of a folder in Java
Changes in Java 11
[Java] Array notes
Rock-paper-scissors in Java
Java SE 7 memo
[Java] Understand in 10 minutes! Associative array and HashMap
java anything memo 2