Java programming things 49-multi-dimensional array use Example 1

Source: Internet
Author: User

Java programming those things 49-multi-dimensional array use Example 1 Zhengzhou game college Chen yuefeng from: http://blog.csdn.net/mailbomb 6.6 multi-dimensional array usage exampleIn actual use, multi-dimensional arrays are more designed for arrays. In actual use, statistics on multi-dimensional arrays are generally less than those on one-dimensional arrays, more design array size, specify the meaning of the stored values in the array, and use the array according to the values in the code. Therefore, before using a multi-dimensional array, you need to consider clearly: l requires several-dimensional arrays l what is the length of each dimension l what are the rules for storing the value l what is the meaning of the array value? 6.6.1 Latin phalanxRequirement: the storage and output of any order of Latin matrix is a regular numerical sequence. For example, the level 4 Latin matrix is as follows: 1 2 3 42 3 4 13 4 1 24 1 2 3 the numbers in the matrix are very regular. To solve this problem, you only need to describe the numerical law. Implementation idea: declare a Variable N, which represents the order of the matrix. Declare and initialize an nxn array. According to the data law, the corresponding value is (row number + column number + 1 ), when the value is greater than N, return the remainder of N. The implementation code is as follows: int n = 6; int [] [] arr = new int [N] [N]; int data; // value // cyclic value assignment for (int row = 0; row <arr. length; row ++) {for (INT Col = 0; Col <arr [row]. length; Col ++) {DATA = row + Col + 1; if (Data <= N) {arr [row] [col] = data ;} else {arr [row] [col] = data % N ;}}// output array value for (int row = 0; row <arr. length; row ++) {for (INT Col = 0; Col <arr [row]. length; Col ++) {system. out. print (ARR [row] [col]); system. out. print ('');} System. out. println ();} in this Code, the variable data stores the value of row number + column number + 1. Each time a value is assigned, it determines whether the value of data is less than or equal to n, assign values to the array element based on the judgment result. When solving practical problems, observing the law of numbers and using programs to express it is also the basic skill required by every programmer. 6.6.2 Yang Hui triangleRequirement: the storage and output of 10 rows of Yang Hui triangle elements are realized. The Yang Hui triangle is a mathematical sequence of numbers. The sequence is as follows: 11 11 2 11 3 3 3 11 4 4 1 the sequence of numbers is, the numbers in the first column of the array are all 1, and the values of each element are equal to the sum of the values of the corresponding element in the previous row and that of the previous element in the previous row. For example, the value of Number 4 in the second column of the fifth line is equal to the sum of element 1 before element 3 in the previous row. Implementation idea: the number of rows in the Yang Hui triangle has several numbers. The number of loops is controlled using the row number. The first row of the internal value is assigned 1, and other values are calculated according to the rule. Assume that the subscript of the array element to be calculated is (row, col), the subscript of the previous element is (Row-1, col), and the subscript of the previous element is (Row-1, col-1 ). The implementation code is as follows: int [] [] arr = new int [10] [10]; // cyclic value assignment for (int row = 0; row <arr. length; row ++) {for (INT Col = 0; Col <= row; Col ++) {If (COL = 0) {// The first arr [row] [col] = 1 ;} else {arr [row] [col] = arr [row-1] [col] + arr [row-1] [col-1] ;}} // output array value for (int row = 0; row <arr. length; row ++) {for (INT Col = 0; Col <= row; Col ++) {system. out. print (ARR [row] [col]); system. out. print ('');} system. out. println ();} this question The rule between two numeric values is relatively simple. It is mainly to understand the basic processing of the subscript of the array, to deepen the understanding of the lower mark of the array, and to control the value of the array element.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.