JAVA basis (two-dimensional array)

Copyright Notice: Copyright: This article is a blogger original article, shall not be reproduced without the bloggers allowed, rights reserved. https://blog.csdn.net/Cricket_7/article/details/91411499

1, two-dimensional array introduction

【1 Overview:

  • Is an array as an array of array elements, namely "array of arrays" on two-dimensional array essence,

  • Type specifier array name [constant expression] [constant expressions].

  • Also known as two-dimensional array matrix , ranks equal number matrix is called square. Symmetric matrix a [i] [j] = a [j] [i],

  • Diagonal matrix: the outer square matrix of order n is zero main diagonal elements.

 

[2] format

  • int [] [] arr = new int [3] [2]; the most used.

  • Explanation

class Demo1_Array {

    public static void main(String[] args) {

        int[][] arr = new int[3][2];

        /*

        这是一个二维数组

        这个二维数组中有3个一维数组

        每个一维数组中有2个元素



        [[I@19bb25a                                    //二维数组的地址值

        [I@da6bf4                                    //一维数组的地址值

        0                                            //元素值

        */

        System.out.println(arr);                    //二维数组

        System.out.println(arr[0]);                    //二维数组中的第一个一维数组

        System.out.println(arr[0][0]);                //二维数组中的第一个一维数组的第一个元素

    }

}

 

 

  • int[][] arr = new int[3][];

  • int[][] arr = {{1,2,3},{4,5},{6,7,8,9}};

[3] Notes

  • The following format may also represent two-dimensional array

    • Data type array name [] [] = new data type [m] [n];

    • Data Type [] array name [] = new data type [m] [n];

  • Note the difference between defined below

            int x; int number of a type of life is variable x 

            int y; int a data type, the variable y is ordered

            int x,y;       

            

            int [] x; int defines a one-dimensional array of data types, designated as x

            int [] y []; defines a two-dimensional array

            

            int [] x, y []; x is a one-dimensional array, y is a two-dimensional array

 

2, illustrates a two-dimensional array 

[Format 1]: int [] [] arr = new int [3] [2]; illustrated

class Demo2_Array {

    public static void main(String[] args) {

        int[][] arr = new int[3][2];

        System.out.println(arr);                    //打印二维数组

        System.out.println(arr[0]);                    //打印二维数组中的第一个一维数组

        System.out.println(arr[0][0]);                //打印二维数组中的第一个一维数组中的第一个元素

    }

}

Two-dimensional array is the address of the value of a one-dimensional array, one-dimensional array to store the elements

  •  

 

[2] format: int [] [] arr = new int [3] [];

class Demo3_Array {

    public static void main(String[] args) {

        int[][] arr = new int[3][];                //这是一个二维数组,这个二维数组中有三个一维数组,三个一维数组都没有被赋值

        

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

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

        System.out.println(arr[2]);

        arr[0] = new int[3];                    //第一个一维数组中可以存储三个int值

        arr[1] = new int[5];                    //第二个一维数组中可以存储五个int值

        System.out.println("------------------");

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

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

        System.out.println(arr[2]);

    }

}
  • Diagram

【3】int[][] arr = {{1,2,3},{4,5},{6,7,8,9}};

class Demo4_Array {

    public static void main(String[] args) {

        int[][] arr = {{1,2,3},{4,5},{6,7,8,9}}; //这是一个二维数组,这个二维数组中每个大括号都代表一个一维数组

        System.out.println(arr);                //[[I@19bb25a,二维数组的地址值

        System.out.println(arr[0]);                //[I@da6bf4,一维数组的地址值

        System.out.println(arr[0][0]);            //1,一维数组中的元素值

    }

}
  • 图解

 

 

Guess you like

Origin blog.csdn.net/Cricket_7/article/details/91411499