I wanted to deepen my understanding of Java's 2D arrays, so I summarized it.
A two-dimensional array is an array that specifies elements with two indexes.
Main.java
public class Main {
public static void main(String[] args) throws Exception {
//How to create an array-⑴ * Explanation
//String[][] teams;
//teams = new String[2][3];
//Declaration of array variables
//Specify the number of elements with the new operator and assign it to teams
//How to create an array- ⑵
//String[][] teams = new String[2][3];
//Perform method ⑴ at the same time
//teams[0][0] = "Brave";
//teams[0][1] = "Warrior";
//teams[0][2] = "Wizard";
//teams[1][0] = "Thieves";
//teams[1][1] = "Ninja";
//teams[1][2] = "merchant";
//Store in array
//How to create an array- ⑶
String[][] teams = {{"Brave", "Warrior", "Wizard"},
{"Thieves", "Ninja", "merchant"}};
//Perform from creation to storage at the same time
System.out.println(teams[0][0]);
//Execution result:Brave
teams[0][0] = "Swordsman";
//Hero → Updated to Swordsman
System.out.println(teams[0][0]);
//Execution result:Swordsman
System.out.println(teams.length);
//Execution result:2
//Array length
System.out.println(teams[0].length);
//Execution result:3
//teams[0]Array length of
}
}
Main.java
teams = new String[n][m];
// n:Number of elements in the array, m:Number of elements in the array stored in the element
In terms of coordinates, the y coordinate is n and the x coordinate is m. For reference, click here!
Recommended Posts