Two dimensional array (understanding)

(1) the element is an array of one-dimensional arrays.

(2) format:

A: data type [] [] array name = new data type [M] [n]; m: indicates how many one-dimensional arrays this two-dimensional array has N: how many elements are there in each one-dimensional array.

B: data type [] [] array name = new data type [M] []; m: indicates how many one-dimensional arrays this two-dimensional array has The number of columns is not given. It can be given dynamically. This time it's a changing number of columns.

C: data type [] [] array name = new data type [] [] { } {... } {... (3)

D: data type [] [] array name = {{ } {... } {... (3)

Be careful:

A: the following format can also represent a two-dimensional array

a: data type array name [] [] = new data type [m][n];

b: data type [] array name [] = new data type [m][n];

B: pay attention to the difference between the following definitions

int x;

int y;

int x,y;

int[] x; int[] y[]; int[] x,y[]; (3)case(master): A:Traversal of two dimensional array //The outer loop controls the length of two-dimensional array, which is actually the number of one-dimensional array. //The inner loop controls the length of the one-dimensional array. /* Requirement: traversing 2D array Two clear: Return value type: void Parameter list: int[][] arr */ public static void printArray2(int[][] arr) { for(int x=0; x<arr.length; x++) { for(int y=0; y<arr[x].length; y++) { System.out.print(arr[x][y]+" "); } System.out.println(); } } B:Sum of two dimensional array C:Yanghui triangle /* Requirement: print Yanghui triangle (number of lines can be entered by keyboard) 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 Analysis: look at the law of this image A:The first and last columns of any row are 1 B:From the third row, each data is the sum of the previous column of its previous row and the current column of its previous row. Steps: A:First, define a two-dimensional array. If the number of rows is n, we define the number of columns as N first. This n data comes from keyboard input. B:Assign 1 to the first and last columns of any row of this 2D array C:Assign values to other elements according to rules From the third row, each data is the sum of the previous column of its previous row and the current column of its previous row. D:Traverse the two-dimensional array. */ import java.util.Scanner; class Array2Test3 { public static void main(String[] args) { //Create keyboard entry object Scanner sc = new Scanner(System.in); //This n data comes from keyboard input. System.out.println("Please enter a data:"); int n = sc.nextInt(); //Define 2D array int[][] arr = new int[n][n]; //Assign 1 to the first and last columns of any row of this 2D array for(int x=0; x<arr.length; x++) { arr[x][0] = 1; //Any row, column 1 arr[x][x] = 1; //Last column of any row } //Assign values to other elements according to rules //From the third row, each data is the sum of the previous column of its previous row and the current column of its previous row. for(int x=2; x<arr.length; x++) { //Here, if y < = x has a small problem, it's the problem in the last column //So we're going to subtract one here //And y should start with 1, because the first column also has a value for(int y=1; y<=x-1; y++) { //Each data is the sum of the previous column of its previous row and the current column of its previous row. arr[x][y] = arr[x-1][y-1] + arr[x-1][y]; } } //Traverse the two-dimensional array. /* for(int x=0; x<arr.length; x++) { for(int y=0; y<arr[x].length; y++) { System.out.print(arr[x][y]+"\t"); } System.out.println(); } */ //At this time, it should be noted that the change of internal circulation must be similar to that of the multiplication table for(int x=0; x<arr.length; x++) { for(int y=0; y<=x; y++) { System.out.print(arr[x][y]+"\t"); } System.out.println(); } } }