Java review ③ (Basic usage of arrays / reference type)
Part 1 "Data structure"
- What is a data structure?
A format that stores data according to certain rules
Part 2 "array"
- What is an array?
A type of data structure
A variable-like box called an element that is lined up continuously.
- Array rules
(1) The types of elements in the array must be the same.
② index (subscript) starts from 0.
- How to write an array
Introduction of characters: Array variables (containers of elements) and elements
① Declaration of array variables
int[ ] nums;
Element type + array variable name
② Create and assign elements
nums = new int[7];
Array variable name = new operator + create 7 elements of int type
- Note that the index of nums at this time is 0 ~ 6.
③ Easy writing
int[] nums = new int[5];
[review]
int [ ] nums;
nums = new int[5];
int [] nums = new int[5];
- How to check the length of the array
nums.length;
- Assignment to element
nums[index] = 30;
- Display the contents of the element.
System.out.print( nums[indes] );
- Characteristics of array initialization
Variables cannot be compiled without initialization.
But the array does not need to be initialized.
The contents of all elements are 0
(To be exact, it is automatically initialized)
- False for double type and null for String.
- Easy way to write an array.
int [ ] nums = new int[3] {10, 20 ,30};
To write it more simply
int [ ] nums = {10, 20, 30};
Part 3 "Combination of array and for statement"
- Turn the array using the for statement.
for ( int i = 0; i < nums.length; I++ ) { System.out.println( nums[ i ] ) }
- Make index a variable instead of a literal (a constant such as 1 or ramen).
- Application of array turning
for ( int i = 0; i < nums.length; i++ ) { sum += nums[ i ] };
for ( int i = 0; I < nums.length; i++ ) {
if ( nums[ i ] > 1 ) {
count++;
}
}
- Extended for statement
for ( int num : nums ) { System.out.print( num ) };
Part 4 "Reference type"
Array variables are called reference variables.
The array variable remembers the start address of the element to be assigned, and when calling the array variable
Using the start address of the element stored in the array variable, refer to the element and output the value of the element.
- Variables such as int type are called basic type. String type is a reference type
Example)
int num;
int num2;
num = 1;
num2 = num;
num2 = 100;
System.out.print (num); → 1 is output.
――――――――――――――――――――――――――――
int [ ] array = {1, 2, 3};
int [ ] array2 = array;
array2[1] = 100;
System.out.print (array [0]); → 100 is output.
- At this time, the start address of the array element is assigned to array2.
The reference destination of array2 and the reference destination of array are the same.
Part 5 "Garbage Collection"
Elements created by using the new operator on array variables are special variables.
For example, if you declare an array in an if statement, the array variable will never be available again after the block ends.
In that case, it is erased without permission, but the element is not erased. So elements that are never referenced put pressure on memory.
Garbage collection is a mechanism that automatically finds and erases such useless things.
Part 6 "Turn off the reference"
In Part 4, I learned that array variables are reference type variables. Breaking a reference means not intentionally referencing this reference.
It's easy to do, assign null to an array variable.
int[ ] array = {1, 2, 3}
array = null;
Part 7 "How to use length"
array.length → number of elements in the array
String.length (); → Number of characters
Part 8 "Multidimensional array"
int[ ] array = new int[ 3 ] [ 3 ];
int [] array = new int [row] [column];
Multidimensional arrays are nested arrays.
The array variable array of int [] [] contains the address of the beginning of the element.
Variables of type int [] [] refer to array variables that contain elements that contain arrays of int [] and int [].
Then, the reference continues by referring to each element of int [].