[JAVA] Array

What is an array?

What is an array
A box that stores multiple data of the same type in order
Caution
-Ordering starts from "0"
-Only the same type of data can be stored in one box

Creating an array

Creation order
① Creating an array variable (creating a box) Element data type [] Array variable name;
② Element creation and substitution (content creation) Array variable name = new Element data type [number of elements]; * When using new
When creating the box and contents at the same time
Element data type [] Array variable name = new Element data type [Number of elements];

Initial value of array

Unlike
variables, arrays have initial values!
Initial values are as follows
Data type initial value
Numeric type 0
boolean type false

Array initialization

You can create and initialize an array with the following notation
Omitted writing ① `Element data type [] Array variable name = new Element data type {Initial value 1, Initial value 2, Initial value 3, Initial value 4, ...};`
Omitted writing ② `Element data type [] Array variable name = {Initial value 1, Initial value 2, Initial value 3, Initial value 4, ...};`

Use an array in a for statement

Examples ``` int[] score = {20,30,40,50,80}; for (int i = 0; i ・ 20, 30, 40, 50, 80 are displayed in order
score.length contains "5"
i contains "0-4"
How to write `for (int i = 0; i Extended for statement
How to write `for (element type arbitrary variable name: array variable name) { }`
→ The contents of the variable are updated every week the loop

How the array is processed behind the scenes

First of all ... ʻInt [] score = new int {5} is executed `
① An array with 5 int type elements is created in memory
② Int [] type array element, score is created in memory
③ The position information in memory at the beginning of the array (0) is assigned to score
After that ... `When score {2}` is executed on the program`
④ The address in memory at the beginning (0) of the score is searched
⑤ Read and write the {2}th memory from the beginning

Garbage collection

Normally, variables created in an if block are not applied when the block disappears. The elements secured by
new do not disappear even after the block ends, and remain as garbage in the memory.
The Java feature that automatically removes these is garbage collection.

null value

	  if ( b== true) {
		int[] i = {1,2,3};
        i=null;
	   }

As described above, by assigning a null value, the memory where the variable value is stored is no longer referenced.

Multidimensional array

Declaration of 2D array `Element type [] [] Array variable name = new Element type [number of rows] [number of columns]`
Description method when using elements of a two-dimensional array `Array variable name [row number] [column number]`
How to initialize a two-dimensional array `Element type [] [] Array variable name = {// 1st row {// 1st column number of elements // 2nd column number of elements // 3rd column number of elements ...}, // 2nd row { }, // 3rd line ...} `