2015-10-13 27 views
3

如何在Java中創建二維字符數組?我已經研究這適用於我可以移動角色的地圖目標,儘管沒有發現任何有用的信息。我之前已經在C++中完成了這個(有一點幫助),但是不知道如何在Java中完成。使用字符串數組在Java中創建2D(地圖)數組

對於C++版本,我開始與一維數組的字符串:

"#######.#.~~~####.##.......~~~..##.~~....H....######..#######", 
    "#####..T.#~~~..##.H......~~~...#..T.~~.........######.###....", 
    "######..~~~~#######...#..~~~.........~~~.##.###..#####.###...", 
    "...###..~~~.######...##[email protected]~~.#.####...##.H.###...", 
    ".#####..~~.~~###C.....#..~~~.....#...~~~..###..#....##....#..", 
    "######....~~~.~.##....#..~~~.....#....~~~.#######C...........", 
    "#######.H..~.~~~~#....#..~~~.....###...~~~.##########........", 
    "########.H...~~~~~.....~~~......H..##....~~~.H######...T...##", 

(你是@符號) ,然後能夠向上突破每個字符串成獨立的字符與2嵌套的for循環將其分解成基本上是二維陣列,你可以移動你的角色。

這是一個很好的做法,如果是的話,怎麼樣? (我花了10個小時試圖獲得基本版本的工作)。有沒有更好的方法來做到這一點?我想稍後用這樣的地圖創建一個非常複雜的遊戲,但需要幫助提出如何。

+0

你會嵌套的for循環通過串的每一行持續的需求,然後把每'.charAt(指數)'到陣列中的位置。記住2D數組是'array [rows] [cols] = string.charAt(x)' – 3kings

回答

1

那麼你需要二維數組爲你的字符串?爲什麼不存儲像「ABCDEFGHI」這樣的字符串並像訪問3x3二維數組那樣訪問它?

地圖x,y「座標」到索引

public class Char2DDemo 
{ 
    public static int ROWS = 3; 
    public static int COLS = 3; 

    public static class Char2D 
    { 
     private StringBuilder sb = null; 
     private int ROWS; 
     private int COLS; 

     public Char2D(String str, int rows, int cols) 
     { 
      this.sb = new StringBuilder(str); 
      this.ROWS = rows; 
      this.COLS = cols; 
     } 

     public StringBuilder getSb() 
     { 
      return sb; 
     } 

     public int getRowCount() 
     { 
      return ROWS; 
     } 

     public int getColCount() 
     { 
      return COLS; 
     } 

     public int xy2idx(int x, int y) 
     { 
      int idx = y * getRowCount() + x; 
      return idx; 
     } 

     public int idx2x(int idx) 
     { 
      int x = idx % getRowCount(); 
      return x; 
     } 

     public int idx2y(int idx) 
     { 
      int y = (idx - idx2x(idx))/getRowCount(); 
      return y; 
     } 

     public Character getCh(int x, int y) 
     { 
      return getSb().charAt(xy2idx(x, y)); 
     } 

     public void setCh(Character ch, int x, int y) 
     { 
      getSb().setCharAt(xy2idx(x, y), ch); 
     } 
    } 

    public static void main(String[] args) throws java.lang.Exception 
    { 
     String test = "ABC" 
        + "DEF" 
        + "GHI"; 

     Char2D c2d = new Char2D(test, 3, 3); 

     System.out.println("Before " + c2d.getSb()); 
     System.out.println("at [2][2] " + c2d.getCh(2, 2)); 
     c2d.setCh('J', 0, 0); 
     c2d.setCh('K', 2, 2); 
     c2d.setCh('M', 1, 1);   
     System.out.println("After " + c2d.getSb()); 
     System.out.println("at [1][0] " + c2d.getCh(1, 0)); 
    } 
} 

輸出:

Before ABCDEFGHI 
at [2][2] I 
After JBCDMFGHK 
at [1][0] B 
+0

所以我一直在閱讀這種方法,只是想澄清它如何處理數組。所以它在技術上只是一個二維數組?那麼如何從2D數組中獲取輸出呢?感謝您的幫助 –

+0

您可以將String看作字符數組。這只是一些特定的數組。 :)每個索引都來自範圍[0..n],其中n是該字符串中的字符數。特定索引0 <= i Willmore

+0

噢好吧,這是有道理的,現在我可以看到他們是如何訪問的,感謝您的幫助! –

1

可以通過執行

char[][] arr = new char[number of rows][number of columns]; 

例如在Java中創建一個二維數組,

char[][] arr = new char[2][3]; 

將創建一個數組,它是2行高和3列寬。因此,通過更改行,可以讓角色在網格中上下移動,並通過更改列來左右移動角色。

1

this問題的答案正是你想要的,但與整數。

char[][] multi = new char[number of lines][number of columns]; 

但我真的建議你使用一些解決方案使用對象來代替。因爲你正在使用Java。

我不知道你的項目,但我想這是一個基於瓷磚的2D遊戲。你可以有一個Tile類,它具有位置信息和瓦片的所有屬性。

另一種方法可用於思考可伸縮性和性能。