Java Basics (2D array)

3, Two dimensional array

1. Overview of 2D arrays

Our big data class of digital plus technology has many students in each class, so it can be stored in an array, and we have many big data classes at the same time.
This should also be stored in an array. How to represent such data? Java provides a two-dimensional array for us to use.
This shows that a two-dimensional array is actually an array whose elements are one-dimensional arrays.

Statement definition format of two-dimensional array:
Data type [] [] variable name = new data type [m][n]; (recommended)
m indicates how many one-dimensional arrays this two-dimensional array has
n represents the number of elements of each one-dimensional array
give an example:
int[][] arr = new int[3][2];
A two-dimensional array arr is defined
This two-dimensional array has three one-dimensional arrays named arr[0],arr[1],arr[2]
Each one-dimensional array has 2 elements, which can be obtained through arr[m][n]
Represents the N + 1st element of the M + 1st one-dimensional array

It is also possible to see the following expressions, which are also correct:
(1),int[] arr[] = new int[3][2];
(2),int arr[][] = new int[3][2];

public class Array2Demo1 {
    public static void main(String[] args) {
        //Define a two-dimensional array and use format 1 for initialization
        int[][] arr = new int[3][2];

        System.out.println(arr);  //  [[I@4554617c
        System.out.println(arr[0]); // [I@74a14482
        System.out.println(arr[1]); // [I@1540e19d
        System.out.println(arr[2]); // [I@677327b6

        //What if you want to get the first element in the first one-dimensional array?
        //First, get the first one-dimensional array
//        int[] array1 = arr[0];
//        //Then get the first element by index
//        System.out.println(array1[0]);
        System.out.println(arr[0][0]);
        System.out.println(arr[0][1]); //Gets the second element in the first one-dimensional array in a two-dimensional array
        //ArrayIndexOutOfBoundsException: 2
//        System.out.println(arr[0][2]); // Gets the second element in the first one-dimensional array in a two-dimensional array

        System.out.println(arr[1][0]);  //Gets the first element in the second one-dimensional array in a two-dimensional array
        System.out.println(arr[1][1]);  //Gets the second element in the second one-dimensional array in a two-dimensional array

        System.out.println(arr[2][0]);  //Gets the first element in the third one-dimensional array in a two-dimensional array
        System.out.println(arr[2][1]);  //Gets the second element in the third one-dimensional array in a two-dimensional array
    }
}

2. The second definition format of two-dimensional array:

Data type [] [] variable name = new data type [m] [];

Example: int[][] arr = new int[3] [];

public class Array2Demo2 {
    public static void main(String[] args) {
        //Defines the second format of a two-dimensional array
        int[][] arr = new int[3][];
        System.out.println(arr);
        System.out.println(arr[0]);
        System.out.println(arr[1]);
        System.out.println(arr[2]);

        //Assign a one-dimensional array to the first element of a two-dimensional array
        //The meaning after assignment is: it represents a two-dimensional array. The first element is a one-dimensional array of int type with length of 4.
        arr[0] = new int[4];
        //The meaning after assignment is: it represents a two-dimensional array. The second element is a one-dimensional array of int type with length of 5.
        arr[1] = new int[5];
        //The meaning after assignment is: it represents a two-dimensional array. The third element is a one-dimensional array of int type with length of 10.
        arr[2] = new int[10];
        System.out.println(arr);  //Address value of two-dimensional array
        System.out.println(arr[0]); //The address value of the first one-dimensional array in a two-dimensional array
        System.out.println(arr[1]); //The address value of the second one-dimensional array in the two-dimensional array
        System.out.println(arr[2]);//Address value of the third one-dimensional array in the two-dimensional array


    }
}

3. The third format in 2D array:

Data type [] [] variable name = new data type [] [] {{element...}, {element...}, {element...}};

Example: int[][] arr = new int[][]{{1,2},{3,4,5},{1,2,34,5};

Simplified format:

Data type [] [] variable name = {element...}, {element...}, {element...}};

Example: int [] [] arr = {1,2}, {3,4,5}, {1,2,34,5};

It means that there are three one-dimensional arrays in the two-dimensional array arr, namely {1,2}, {3,4,5}, {1,2,34,5}

public class Array2Demo3 {
    public static void main(String[] args) {
        //The third format definition of array
        int[][] arr = {{1,2},{3,4,5},{1,2,34,5}};
        System.out.println(arr); //Address value of two-dimensional array
        System.out.println(arr[0]); //Two dimensional array the address value of the first one-dimensional array
        System.out.println(arr[1]);//Two dimensional array the address value of the second one-dimensional array
        System.out.println(arr[2]);//Two dimensional array address value of the third one-dimensional array

        //Gets the first element of the first one-dimensional array in a two-dimensional array
        System.out.println(arr[0][0]);
        System.out.println(arr[0][1]);

        //Gets all the elements in the second one-dimensional array in a two-dimensional array
        System.out.println(arr[1][0]);
        System.out.println(arr[1][1]);
        System.out.println(arr[1][2]);

        //Gets all the elements in the third one-dimensional array in a two-dimensional array
        System.out.println(arr[2][0]);
        System.out.println(arr[2][1]);
        System.out.println(arr[2][2]);
        System.out.println(arr[2][3]);
//        System.out.println(arr[2][4]);

    }
}

4. Example: two dimensional array traversal

Digital plus Technology

Thirteen issues

Xiao Wang, Xiao Li

Fourteenth issue

Xiao Ming, Xiao Liu, Xiao Zhang

Fifteenth issue

Xiao Hu, Xiao Yu, Xiao Wang, Xiao Kai

Sixteenth issue

Xiao Huo, Xiao Yang, Xiao AI, Xiao Zhou

public class Array2Demo4 {
    public static void main(String[] args) {
        //Defining a two-dimensional array representation is a number plus technique
        String[][] shujia = {{"Xiao Wang", "petty thief"}, {"Xiao Ming", "Xiao Liu", "Xiao Zhang"}, {"Little tiger", "Xiaoyu", "Xiao Wang", "Xiao Kai"}, {"Small fire", "Xiao Yang", "Little love", "Xiao Zhou"}};

        for (int i = 0; i < shujia.length; i++) {
//            System.out.println(shujia[i]);
            //The outer loop traverses each one-dimensional array
            //To get the elements in each one-dimensional array, do another for loop traversal
            System.out.println("The first" + (i + 1) + "Arrays:");
            for (int j = 0; j < shujia[i].length; j++) {
//                System.out.println(shujia[i][j]);
                if (j == shujia[i].length - 1) {
                    System.out.print(shujia[i][j] + "]");
                } else if (j == 0) {
                    System.out.print("[" + shujia[i][j] + ",");
                } else {
                    System.out.print(shujia[i][j] + ",");
                }
            }
            //The end of each inner loop means that a one-dimensional array is printed and a line feed is performed
            System.out.println();

        }


    }
}

5. Example: sum of annual sales of the company

The statistics of a company by quarter and month are as follows: unit (10000 yuan)

First quarter: 22,66,44

Second quarter: 77,33,88

Third quarter: 25,45,65

Fourth quarter: 11,66,99

public class Array2Demo5 {
    public static void main(String[] args) {
        //Define a two-dimensional array to represent a company. Each quarter is equivalent to a one-dimensional array passed in as the elements of the two-dimensional array
        int[][] arr = {{22, 66, 44}, {77, 33, 88}, {25, 45, 65}, {11, 66, 99}};

//        int[][] arr = new int[2][]{{1,2},{3,1,2}};

        //Define a variable to count total sales
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                sum = sum + arr[i][j];
            }
        }

        System.out.println("The company's annual sales are:" + sum);


    }
}

Keywords: Java Algorithm data structure

Added by lkalik on Mon, 10 Jan 2022 02:51:14 +0200