Java programming 48-multi-dimensional array Basics

Source: Internet
Author: User

Java programming those things 48-multidimensional array based Zhengzhou game college Chen yuefeng from: http://blog.csdn.net/mailbomb 6.4 multi-dimensional array BasicsIn the school, because the number of students in a certain class is small, the numbers can be numbered in sequence. When the number of students increases, for example, the number of students in the school needs to be increased, for example, XX class xx. This is also true in the Army. The XX division XX regiment XX battalion XX connects xx to xx class, And the level here is quite deep. To facilitate data management, we generally need to deepen the management level, which is the origin of multi-dimensional arrays. Multi-dimensional array: an array of two or more dimensions. A two-dimensional array has two layers, a three-dimensional array has three layers, and so on. Each layer corresponds to a subscript. In practice, to make the structure clear, multidimensional arrays are usually used for complex data. To understand multi-dimensional arrays, we finally understand the concept of arrays. Because arrays are a composite data type, Arrays can also be used as array elements. In this way, the two-dimensional array can be understood as each element inside is a one-dimensional array of the one-dimensional array type. A 3D array can be considered as a one-dimensional array. Each element inside is a two-dimensional array. Both the logic and syntax support the "array" Understanding method. Generally, the first dimension of a two-dimensional array represents the row and the second dimension represents the column. This logical structure is consistent with the actual structure. Similar to a one-dimensional array, because a multi-dimensional array has multiple subscripts, you must specify multiple subscripts When referencing elements in the array. 6.5 multi-dimensional array syntaxThe following uses a two-dimensional array as an example to introduce the syntax of a multi-dimensional array. 6.5.1 multi-dimensional array declarationMulti-dimensional array declaration: Data Type [] [] array name; Data Type [] array name []; data type array name [] []; the functions of the preceding three syntaxes when declaring a two-dimensional array are equivalent. Similarly, three pairs of brackets are required to declare a three-dimensional array. The brackets can be placed after the data type, after the array name, and so on. For example, int [] [] map; char C [] []; is the same as a one-dimensional array. After the array declaration, no specific storage space is allocated in the memory, the length of the array is not set. 6.5.2 multi-dimensional array InitializationLike a one-dimensional array, multi-dimensional array initialization can also be divided into static initialization (overall assignment) and dynamic initialization. The syntax format is as follows. 6.5.2.1 static InitializationTake static initialization of two-dimensional arrays as an example to describe the syntax format of static initialization of multi-dimensional arrays. The sample code is as follows: int [] [] M = {1, 2, 3}, {2, 3}; During static initialization of a two-dimensional array, it must also be written together with the array declaration. When writing a value, two braces are nested to write the value of a number in the innermost braces. Values are separated by commas, and internal braces are also separated by commas. From this syntax, we can see that the internal braces are actually static initialization of a one-dimensional array, while a two-dimensional array only combines static initialization of multiple one-dimensional arrays. Likewise, the static initialization syntax format for 3D arrays is as follows: int [] [] [] B ={{ 1, 2, 3 },{ 1, 2, 3 }},{ {3, 4, 4, 1 },{ 2, 3, 4 }}; Note: Only the syntax format is demonstrated, and the value itself is meaningless. 6.5.2.2 dynamic InitializationSyntax format for dynamic initialization of two-dimensional arrays: Data Type [] [] array name = new data type [length of the first dimension] [length of the second dimension]; data Type [] [] array name; array name = new data type [length of the first dimension] [length of the second dimension]; sample code: byte [] [] B = new byte [2] [3]; int M [] []; M = new int [4] [4]; same as a one-dimensional array, dynamic initialization can be separated from the array declaration. Dynamic initialization only specifies the length of the array. The initialization of each element in the array is the default value of the Data Type During array declaration. For example, array B with 2x3 and array m with 4x4 are initialized above. Using this method, the length of the initialized 2D array is the same. to initialize a 2D array with different 2D lengths, you can use the following format: int N [] []; N = new int [2] []; // only initialize the length of the first dimension // initialize the subsequent element N [0] = new int [4]; N [1] = new int [3]; The syntax here reflects the array concept. When initializing the length of the first dimension, in fact, the array n is regarded as a one-dimensional array and its initial length is 2. The two elements contained in array n are N [0] and N [1], respectively. the two elements are a one-dimensional array. The following uses the one-dimensional array dynamic initialization syntax to initialize N [0] and N [1] respectively. 6.5.3 reference array elementsFor a two-dimensional array, because it has two subscripts, the format of referenced array element values is: array name [] [] The expression type is the same as the data type when the array is declared. For example, when referencing elements in a two-dimensional array m, use M [0] [0] to reference the element where the first-dimensional subscript in the array is 0 and the second-dimensional subscript is also 0. Here, the interval between the first-dimensional submark is the length between 0 and the first-dimensional minus 1, and the interval between the second-dimensional submark is the length between 0 and the second-dimensional minus 1. 6.5.4 get the array LengthFor multi-dimensional arrays, you can also obtain the length of the array. However, the array name. length is used to obtain the length of the first dimension of the array. To obtain the total number of elements in a two-dimensional array, use the following code: int [] [] M = {1, 2, 3}, {1}, {3}, {3, 4, 2 }}; int sum = 0; For (INT I = 0; I <m. length; I ++) {// The first-dimensional subscript of the loop sum + = m [I]. length; // Add the second-dimensional length} in this Code, M. length indicates the length of the first dimension of the M array. The internal M [I] refers to each one-dimensional array element, M [I]. length is the length of the M [I] array. Adding these lengths is the total number of elements in array M.
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.