(50)多维数组使用示例2

最后更新于:2022-04-01 10:59:13

## Java编程那些事儿50—多维数组使用示例2 郑州游戏学院 陈跃峰 出自:[http://blog.csdn.net/mailbomb](http://blog.csdn.net/mailbomb) **6.6.3 存储图形结构** 要求:根据数组中的值,在对应位置绘制指定的字符。规定0绘制空格,1绘制星号(*)。数组的值如下所示:                             {                                      {0,0,0,1,0,0,0},                                      {0,0,1,0,1,0,0},                                      {0,1,0,0,0,1,0},                                      {1,0,0,0,0,0,1},                                      {0,1,0,0,0,1,0},                                      {0,0,1,0,1,0,0},                                      {0,0,0,1,0,0,0}                             } 该题目是一个基本的数组应用,数组中的值存储的是控制信息,程序根据数组中的值实现规定的功能。 实现思路:循环数组中的元素,判断数组中的值,根据值绘制对应的字符即可。 实现的代码如下所示: ~~~                    int[][] map = {                                      {0,0,0,1,0,0,0},                                      {0,0,1,0,1,0,0},                                      {0,1,0,0,0,1,0},                                      {1,0,0,0,0,0,1},                                      {0,1,0,0,0,1,0},                                      {0,0,1,0,1,0,0},                                      {0,0,0,1,0,0,0}                    };                    //输出数组的值                    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();                    } ~~~ 类似的代码在游戏开发中,可以用来代表游戏中的地图数据,或者俄罗斯方块等益智游戏中地图块的值。 **6.6.4 螺旋数组** 要求:存储和输出nXm的螺旋数组,其中n和m为大于0的整数。 以下是一些螺旋数组的示例: 1          2  3  4                     1  2  3  4  5 12 13 14 5                     14  15  16  17 6 11  16 15 6                     13  20  19  18  7 10 9  8  7                     12  11  10   9  8 4X4螺旋数组                        4X5螺旋数组 对于螺旋数组来说,其中的数值很有规则,就是按照旋转的结构数值每次加1,实现该功能需要对数组和流程控制有角深刻的认识。 实现思路:声明一个变量来代表需要为数组元素赋的值,对于其中的数字来说,每个数字都有一个移动方向,这个方向指向下一个元素,根据该方向改变数组的下标,如果到达边界或指向的元素已经赋值,则改变方向。 实现代码如下: ~~~                   int n = 4;                    int m = 5;                    int[][] data = new int[n][m];                    int dire;   //当前数字的移动方向                    final int UP = 0;   //上                    final int DOWN = 1; //下                    final int LEFT = 2; //左                    final int RIGHT = 3;//右                    dire = RIGHT;                    int value = 1;    //数组元素的值                    int row = 0;     //第一维下标                    int col = 0;     //第二维下标                    data[0][0] = 1; //初始化第一个元素                    while(value < n * m){                             switch(dire){                                      case UP:                                                row--; //移动到上一行                                                if(row < 0){ //超过边界                                                         row++; //后退                                                         dire = RIGHT;                                                         continue; //跳过该次循环                                                }else if(data[row][col] != 0){//已赋值                                                         row++; //后退                                                         dire = RIGHT;                                                         continue; //跳过该次循环                                                }                                                break;                                      case DOWN:                                                row++; //移动到下一行                                                if(row >= n){ //超过边界                                                         row--; //后退                                                         dire = LEFT;                                                         continue; //跳过该次循环                                                }else if(data[row][col] != 0){//已赋值                                                         row--; //后退                                                         dire = LEFT;                                                         continue; //跳过该次循环                                                }                                                break;                                      case LEFT:                                                col--; //移动到前一列                                                if(col < 0){ //超过边界                                                         col++; //后退                                                         dire = UP;                                                         continue; //跳过该次循环                                                }else if(data[row][col] != 0){//已赋值                                                         col++; //后退                                                         dire = UP;                                                         continue; //跳过该次循环                                                }                                                break;                                      case RIGHT:                                                col++; //移动到后一行                                                if(col >= m){ //超过边界                                                         col--; //后退                                                         dire = DOWN;                                                         continue; //跳过该次循环                                                }else if(data[row][col] != 0){//已赋值                                                         col--; //后退                                                         dire = DOWN;                                                         continue; //跳过该次循环                                                }                                                break;                             }                             value++; //数值增加1                             data[row][col] = value;//赋值                                       }                    //输出数组中的元素                    for(int i = 0;i < data.length;i++){                             for(int j = 0;j < data[i].length;j++){                                      if(data[i][j] < 10){//右对齐                                                System.out.print(' ');                                      }                                      System.out.print(data[i][j]);                                      System.out.print(' ');                             }                             System.out.println();                    } ~~~ 在该代码中dire代表当前元素的移动方向,每个根据该变量的值实现移动,如果移动时超出边界或移动到的位置已赋值,则改变方向,并跳过本次循环,如果移动成功,则数值增加1,对数组元素进行赋值。 对于多维数组来说,更多的是设计数组的结构,并根据逻辑的需要变换数组的下标,实现对于多维数组元素的操作。
';