Java編程那些事兒50—多維陣列使用樣本2

來源:互聯網
上載者:User
 

Java編程那些事兒50—多維陣列使用樣本2鄭州遊戲學院 陳躍峰出自: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  512 13 14 5                     14  15  16  17 611  16 15 6                     13  20  19  18  710 9  8  7                     12  11  10   9  84X4螺旋數組                        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,對數組元素進行賦值。         對於多維陣列來說,更多的是設計數組的結構,並根據邏輯的需要變換數組的下標,實現對於多維陣列元素的操作。
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.