-A array is a data structure that stores multiple data of the same type in the order in which they are arranged. -The array contains multiple elements, and the elements have subscripts (index) in order from 0. -Arrays are similar to variables and specify the data type.
java
Element data type[]Array variable name;
#For example
int[] score; #This declares score as an int type array variable.
score = new int[5]; #Five int type elements generated by the new operator are assigned to the array variable score.
int[] score = {10,20,30,40,50}; #Declare and initialize at the same time.
#It is also possible to declare and assign an array variable at the same time.
int[] score = new int[5];
#How to retrieve elements in an array
score[0]; #You can get the first element in the array variable score.
#Rewrite the elements in the array.
score[0] = 30; #A value of 30 can be assigned to the first element of the array variable score.
#use length method
score.length; #You can get the number of elements in an array by using a method called length for an array variable.
#The length method can also be used for strings. String type variable name.length()It becomes the form.
java
#Traditional for statement
for (int i = 0; i <Array variable name.length; i++) {
processing....
} #The number of elements in the array is acquired by length, and the loop is rotated by that number.
#Extended for statement
for (Element type Arbitrary variable name:Array variable name) {
processing...
} #Writing with an extended for statement makes it simpler than the conventional one.
#For example
for (int value : score) {
processing...
}
The computer records the data it uses in memory. The inside of the memory is organized like the eyes of the base, and addresses are assigned to each section. And when you declare a variable, it reserves an empty partition (you don't know which one is chosen) for the variable (how many partitions are used depends on the variable shell). Assigning a value to a variable means recording the value in the reserved partition. The first address of the first element is assigned to the array variable. In other words, the array variable does not contain elements, and the elements belonging to the array variable can be obtained by referring to the address of the array variable.
One of the mechanisms of Java. It eliminates dust in memory. Unused variables and arrays waste memory and put pressure on it. Originally, the programmer has to clean up this memory garbage, but since the garbage collection mechanism is always working, the memory garbage is automatically cleaned up.
NULL Means nothing. By assigning NULL to an array variable that is no longer used, the array variable no longer refers to the element and becomes the target of garbage collection. Can be used for reference type variables but not for base type variables.
java
int[] score = {10,20,30}; #Assign a value to an element of an array variable.
int[] score = null; #Assign NULL to the array variable.
score[0]; #The array variable contains NULL and cannot be referenced.
↓ A two-dimensional array is an array like a table (I'm sorry if it's hard to understand)
[0][0] | [0][1] | [0][2] | [0][3] |
---|---|---|---|
[1][0] | [1][0] | [1][2] | [1][3] |
[2][0] | [2][1] | [2][2] | [2][3] |
java
#Declaration of a two-dimensional array
Element type[][]Array variable name= new Element type[Number of lines][Number of columns]
#Get elements of a two-dimensional array
Array variable name[Line subscript][Column subscript]
Recommended Posts