Java array

Learning log

Array

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

Turn the array in a for loop

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);
    }
  }
}

Memory variables and arrays

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

Garbage collection

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

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

[Java] array
Java array
Java array
java (array)
Java array
[Java] Array
Java array
java array
[Java] Array
java array variable
[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 protected
[Java] Annotation
[Java] Module
[Ruby] Array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Array practice
Java methods
Java method
java (constructor)
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
Java learning memo (creating an array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
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