Java, about 2D arrays

Introduction

I wanted to deepen my understanding of Java's 2D arrays, so I summarized it.

Sample code

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

Commentary

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

Java, about 2D arrays
[Java] About arrays
About Java arrays
[Java] About Java 12 features
Something about java
Where about java
About Java features
About Ruby arrays
About Java threads
[Java] About interface
About Java class
About java inheritance
About interface, java interface
About List [Java]
About java var
About Java literals
About Java commands
About Java log output
About Java functional interface
About class division (Java)
Combine arrays in Java
About [Java] [StreamAPI] allMatch ()
About Java StringBuilder class
[Java] About Singleton Class
About Java method binding
[Java] About anonymous classes
About method splitting (Java)
[Java Silver] About initialization
About Java Array List
About Java Polymorphism super ()
About inheritance (Java Silver)
About Java String class
About Java access modifiers
About Java lambda expressions
About Java entry points
About Java 10 Docker support
Personal summary about Java
[Java] About enum type
All about Java programming
About java abstract class
Java Welcome to the Swamp of 2D Arrays
A note about Java GC
What I researched about Java 8
About an instance of java
Study java arrays, lists, maps
What I researched about Java 6
[Gradle] About Java plug-in tasks
About Java variable declaration statements
What I researched about Java 9
[Java] About try-catch exception handling
About Java class loader types
[Java Silver] About equals method
[Java] About String and StringBuilder
What I researched about Java 7
About Alibaba Java Coding Guidelines
About Java class variables class methods
About Java Packages and imports
About abstract classes in java
[Android / Java] Learned about DataBinding
What I researched about Java 5
About Java static and non-static methods