The concept of two dimensional array

###The essence of two dimensional array
The essence is to store an array of one-dimensional arrays
Format 1:
Data type [] array name = new data type [array length];
Data type [] [] array name = new data type [m][n]; m: length of one-dimensional array in two-dimensional array
n: number of elements in each one-dimensional array

int[][] arr = new int[3][2];
Indicates that there are three one-dimensional arrays in the arr, and each one has two elements

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

Format 2:
Array type [] [] array name = new data type [m] [];
m: length of one-dimensional array in two-dimensional array
n: m is necessary, n can be omitted, indicating that the number of elements of each one-dimensional array is uncertain
The number of elements of each one-dimensional array can be dynamically changed later

int[][] arr = new int[3][2];
		System.out.println(arr); // Address [[I@7852e922
		
		System.out.println(arr[0]); // Address [I@4e25154f
		System.out.println(arr[1]); // Address [I@70dea4e
		System.out.println(arr[2]); // Address [I@5c647e05
//		System.out.println(arr[3]); / / array out of bounds
		
		System.out.println(arr[0][0]); // 0
		System.out.println(arr[0][1]); // 0
		System.out.println(arr[1][0]); // 0
//		System.out.println(arr[1][2]); / / array out of bounds

###Static initialization of 2D array

int[][] arr = {{11,22,33} , {44,55} , {66,77,88,99} };
System.out.println(arr);//address

//Traversal method of two dimensional array
//Outer loop controls the first one dimensional array in two dimensional array
		for (int i = 0; i < arr.length; i++) {
			//Inner loop controls the elements of the first bit array
			for (int j = 0; j < arr[i].length; j++) {
				System.out.println(arr[i][j]);
			}
		}
public static void main(String[] args) {
		int[][] arr = {{1,7,9,11,13,15,17,19},{2,4,6,8,10}};
		int[] newArr = Arrays.copyOf(arr[1], arr[0].length+arr[1].length);
		System.arraycopy(arr[0], 0, newArr, 5, arr[0].length);
		Arrays.sort(newArr);
		System.out.println(Arrays.toString(newArr));
	}
	//[4, 15, 20, 27][2, 21, 32, 47, 65, 89]

###Two dimensional array printing Yanghui triangle

//Keyboard input print n lines Yang Hui triangle
/*
 * 1				arr[0][0]              
 * 1 1				arr[1][0]	arr[1][1]				
 * 1 2 1			arr[2][0]	arr[2][2]
 * 1 3 3 1			arr[3][0]	arr[3][3]
 * 1 4 6 4 1		arr[4][0]	arr[4][4]
 * 
 * n Row n column       
 * The values of the first and last columns are 1
 * Starting from the third row, each value is the value of the previous column of the previous row plus the value of this column of the previous row
 */
public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int n = input.nextInt();
		int[][] arr = new int[n][n];
		for (int i = 0; i < arr.length; i++) {
			arr[i][0] = 1;
			arr[i][i] = 1;
			
		}
		for (int i = 2; i < arr.length; i++) {
			for (int j = 1; j < arr.length - 1; j++) {
				arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
			}
		}
		newArrays(arr);
	}

	private static void newArrays(int[][] arr) {
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j <= i; j++) {
				System.out.print(arr[i][j]+"\t");
			}
			System.out.println();
		}
	}
	

Posted by Ryan Sanders on Tue, 26 Nov 2019 08:45:19 -0800