[Java] Java array

1 minute read

Introduction

Even if it is said that you can make it by progressive, repeating, and conditional branching, you can’t make it at all! !! (Not related to this post)

Array

** Variables can only contain one value **, but ** arrays can contain multiple values ** together.
One column is called ** one-dimensional array **, and two or more columns are called ** multidimensional array **.
Each one is called ** element **, and the number is called ** element number **.

kane.java


Data type[]Array variable name;
Array variable name=new data type[Element count];

String[] banana;
banana = new string[3];

banana[0] = "Cavendish"
banana[1] = "Lakatan"
banana[2] = "GrosMichel"

The above describes the declaration and assignment separately
Below is the declaration and assignment at the same time

kane.java


Data type[]Array variable name= {Element 1,Element 2,...};

String[] banana = {"Cavendish", "Lakatan", "GrosMichel"}

Write [] after the data type (like ** int [], String [] **)

** Element overwrite is array variable name [index number] = “new element” **

Each element can be obtained ** by the array name [index number] **.

kane.java


String[] banana = {"Cavendish", "Lakatan", "GrosMichel"};

banana[1] = "LadyFinger" //Overwriting elements

System.out.println(banana[1]);

//Execution result Lady Finger

Multidimensional array

Excel spreadsheet software

banana 0 1
0 Cavendish Lakatan
1 GrosMichel LadyFinger

kane.java


Data type[][]Array variable name;
Array variable name=new data type[Element count][Element count];

String[][] banana;
banana = new string[2][2];

banana[0][0] = "Cavendish"
banana[0][1] = "Lakatan"
banana[1][0] = "GrosMichel"
banana[1][1] = "LadyFinger"

System.out.println(banana[1][0]);

//Execution result GrosMichel

This can also be omitted

kane.java


String[][] banana = {{"Cavendish", "Lakatan"},{"GrosMichel","LadyFinger"}};

System.out.println(banana[0][0]);

//Execution result Cavendish

At the end

I understand that multidimensional arrays are convenient, but I get a flat tire because my head is one-dimensional.

Tags:

Updated: