Java array

What is an array?

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

Turn the array in a for loop.

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

Memory, variables and arrays

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.

Garbage collection

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.

Two-dimensional array

↓ 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

[Java] array
Java array
Java array
java (array)
Java array
[Java] Array
Java array
java array
[Java] Array
[Java] Array notes
Array
About Java Array List
Java
[Java ~ Array ~] Study memo 4
Java
Array
[Java] List type / Array type conversion
Java Development Basics ~ Exercise (Array) ~
[Java] Convert ArrayList to array
[Java Silver] Array generation method
How to initialize Java array
[Beginner] Java basic "array" description
Java learning (0)
Studying Java ―― 3
[Java] Annotation
[Java] Module
[Ruby] Array
Java tips, tips
Array practice
Java methods
Java method
java (constructor)
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java learning memo (creating an array)
Java static
Java serialization
JAVA paid
Java (set)
java shellsort
Studying Java -5
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Studying Java ―― 1
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
java (encapsulation)
Java inheritance
[Java] Overload