Java programming-50-Example 2 of multi-dimensional array

Source: Internet
Author: User

Java programming those things 50-multi-dimensional array use Example 2 Zhengzhou game college Chen yuefeng from: http://blog.csdn.net/mailbomb 6.6.3 storage image structureRequirement: Draw the specified character at the corresponding position based on the value in the array. Specify 0 to draw spaces, and 1 to draw an asterisk (*). The array values are as follows: {0, 0, 0, 0}, {0, 0, 1, 0, 0}, {, 0, 0, 0, 0, 0, 0, 1}, {0, 0, 0, 0}, {0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0} this topic is a basic Array application. The values in the array store control information, and the program implements the required functions according to the values in the array. Implementation: loop the elements in the array, judge the values in the array, and draw the corresponding characters based on the values. The implementation code is as follows: int [] [] map = {0, 0}, {0, 0}, {0, 0, 0, 0, 0}, {, 0, 0, 0, 0, 1}, {0, 0, 0, 0}, {0, 0}, {0, 0, 0, 0 }}; // output array value for (int row = 0; row <map. length; row ++) {for (INT Col = 0; Col <map [row]. length; Col ++) {Switch (Map [row] [col]) {Case 0: system. out. print (''); break; Case 1: system. out. print ('*'); break;} system. out. println ();} similar code can be used in game development to represent map data in the game, or Values of map blocks in Russian square and other puzzle games. 6.6.4 spiral ArrayRequirements: store and output nxm spiral arrays, where n and m are integers greater than 0. The following is an example of a spiral array: 1 2 3 4 1 2 3 4 512 13 14 5 14 15 16 17 611 16 15 15 6 13 20 19 18 710 9 8 12 11 10 9 84x4 spiral Array 4x5 spiral array for Spiral array, the numeric value is quite regular, that is, adding 1 at a time according to the rotating structure value. To achieve this function, you need to have a deep understanding of array and flow control. Implementation idea: declare a variable to indicate the value to be assigned to the array element. For the number, each number has a moving direction, which points to the next element, change the subscript of the array based on this direction. If a value has been assigned to the boundary or the element to which it points, the direction is changed. The implementation code is as follows: int N = 4; int M = 5; int [] [] DATA = new int [N] [m]; int dire; // The moving direction of the current number is final int up = 0; // The top is final int down = 1; // The bottom is final int left = 2; // The Left is final int right = 3; // right dire = right; int value = 1; // The value of the array element int ROW = 0; // The first dimension subscript int Col = 0; // The second-dimensional subscript data [0] [0] = 1; // initialize the first element while (value <n * m) {Switch (dire) {Case up: row --; // move to the previous row if (row <0) {// exceeds the boundary row ++; // returns dire = right; continue; // Skip this loop} else if (data [row] [col]! = 0) {// row ++ with a value assigned; // return dire = right; continue; // skip this loop} break; case down: Row ++; // move to the next row if (row >=n) {// exceeds the boundary row --; // returns dire = left; continue; // skip this loop} else if (data [row] [col]! = 0) {// row --; // returns dire = left; continue; // skips this loop} break; case left: Col --; // move to the previous column if (COL <0) {// exceeds the boundary Col ++; // returns dire = up; continue; // skip this loop} else if (data [row] [col]! = 0) {// assigned Col ++; // returns dire = up; continue; // skips this loop} break; case right: Col ++; // move to the next row if (COL> = m) {// exceeds the boundary col --; // returns dire = down; continue; // skip this loop} else if (data [row] [col]! = 0) {// assigned col --; // returns dire = down; continue; // skips this loop} break;} value ++; // increase the value by 1 DATA [row] [col] = value; // value assignment} // output the element for (INT I = 0; I <data. length; I ++) {for (Int J = 0; j <data [I]. length; j ++) {If (data [I] [J] <10) {// right-aligned system. out. print ('');} system. out. print (data [I] [J]); system. out. print ('');} system. out. println ();} in this Code, dire represents the direction of movement of the current element. Each element is moved based on the value of this variable. If the value is greater than the boundary or has been assigned a value, then change the direction and skip this loop. If the operation succeeds, the value is increased by 1 and the array element is assigned a value. For multi-dimensional arrays, the structure of the array is designed, and the subscript of the array is changed according to the logic needs to implement operations on multi-dimensional array elements.

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.