An array is a data structure that stores multiple data of the same type in the order in which they are arranged. Only the same type of data can be stored in each element of the array. The elements of the array start at 0. Arrays specify data types as well as variables.
python
Creating (declaring) array variables
Element type[]Array variable name
//Example
int[] score; //Declare 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.
//It is also possible to declare array variables and create and assign elements at the same time.
int[] score = new int[5];
//How to retrieve elements in an array
score[0]; //Get the first element in the array variable score
//Change elements in an array
score[0] = 50; //You can assign the value 30 to the first element in the array variable score
//Get the number of elements in an array
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.
//Array abbreviation
int[] score1 = new int[] {10, 20, 30, 40, 50};
int[] score2 = {10, 20, 30, 40, 50};
python
//Traditional for statement
for(int i = 0; i <Array variable name.length; i++) {
processing..
}
//Traditional for statement example
public class Main {
public static void main(String[] args) {
int[] score = {20, 30, 40, 50, 60};
for(int i = 0; i < score.length; i++) {
System.out.println(score[i]);
}
}
}
//Extended for statement
for(Element type Arbitrary variable name:Array variable name) {
processing..
}
//Extended for statement example
public class Main {
public static void main(String[] args) {
int[] score = {20, 30, 40, 50, 60};
for(int value : score) {
System.out.println(value);
}
}
}
The computer records the data it uses in memory. The memory is organized like a grid, and each section is assigned an address. And when you declare a variable, it reserves an empty partition (I don't know where it will be chosen) for the variable (how many partitions are used depends on the type of variable). Assigning a value to a variable means recording the value in the reserved partition.
python
public class Main {
public static void main{String[] args) {
int[] a = {1, 2, 3};
int[] b = b;
b = a;
b[0] = 100;
System.out.println(a[0]); //Output as 100
}
}
One of the mechanisms of java. It automatically finds and cleans up the garbage in the memory (= memory area that is no longer referenced by any variable) generated by the running program.
NULL It means "nothing". When null is assigned, the reference type variable is not referenced anywhere. -If you assign to a reference type variable such as int [] type, that variable will not refer to anything. -Cannot be assigned to basic type variables such as int type.
python
int[] score = {1,2,3}
score = null;
score[0];
A two-dimensional array is an image in which elements are arranged vertically and horizontally.
[0][0] | [0][1] | [0][2] | [0][3] |
---|---|---|---|
[1][0] | [1][1] | [1][2] | [1][3] |
[2][0] | [2][1] | [2][2] | [2][3] |
python
//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]
//Example
int[][] scores = new int[2][3]; //2-by-3 array
scores[0][0] = 40;
scores[0][1] = 50;
scores[0][2] = 60;
scores[1][0] = 70;
scores[1][1] = 80;
scores[1][2] = 90;
System.out.println(scores[1][1]); //80
Recommended Posts