Diagram of two-dimensional array
Second, plot dynamic creation of two-dimensional arrays
Three, Yang Hui triangle exercises:
1 ImportJava.util.Scanner;2 classarray2demo{3 //Realizing the Yang Hui Triangle4 Public Static voidMain (string[] args) {5 //Creating a Keyboard entry object6Scanner sc=NewScanner (system.in);7System.out.println ("Please enter data:");8 intn=sc.nextint ();9 Ten //defining a two-dimensional array One int[] arr=New int[n][n]; A //assign a value of 1 to the first and last column of each row of this two-dimensional array - for(intx=0;x<arr.length;x++){ -Arr[x][0]=1;//first column per row theArr[x][x]=1;//last column of each row - } - //rule: Starting with the third row, the value of each element is the sum of the previous column and the value of the column in the previous row - for(intx=2;x<arr.length;x++){ + //because the first and last columns in each row are assigned a value of 1, you should traverse the column by 1 minus - for(inty=1;y<=x-1;y++){ + //Assign values by law AArr[x][y]=arr[x-1][y-1]+arr[x-1][y]; at } - } - //Print Output - for(inti=0;i<arr.length;i++){ - for(intj=0;j<=i;j++){ - System.out.print (Arr[i][j]); in } - System.out.println (); to } + } -}
Java Beginner (iv)