【问题标题】:Using an array of Strings to create a 2D (map) array in Java使用字符串数组在 Java 中创建二维(地图)数组
【发布时间】:2023-04-08 18:31:01
【问题描述】:

如何在 Java 中创建一个二维字符数组?我正在研究这是否适用于我的地图目标,您可以在地图上移动角色,但没有发现任何有用的东西。我以前用 C++ 做过这个(有一点帮助),虽然不知道如何用 Java 做。

对于 C++ 版本,我从一维字符串数组开始:

    "#######.#.~~~####.##.......~~~..##.~~....H....######..#######",
    "#####..T.#~~~..##.H......~~~...#..T.~~.........######.###....",
    "######..~~~~#######...#..~~~.........~~~.##.###..#####.###...",
    "...###..~~~.######...##H.~~~.@........~~.#.####...##.H.###...",
    ".#####..~~.~~###C.....#..~~~.....#...~~~..###..#....##....#..",
    "######....~~~.~.##....#..~~~.....#....~~~.#######C...........",
    "#######.H..~.~~~~#....#..~~~.....###...~~~.##########........",
    "########.H...~~~~~.....~~~......H..##....~~~.H######...T...##",

(你是@符号) 然后能够将每个字符串分解为单独的字符,使用 2 个嵌套的 for 循环将其分解为基本上可以移动角色的二维数组。

这是一个很好的方法吗,如果是的话怎么做? (我现在花了 10 个小时试图让一个基本版本工作)。有没有更好的方法来做到这一点?我想稍后用这样的地图创建一个非常复杂的游戏,但需要帮助来解决问题。

【问题讨论】:

  • 您需要嵌套的 for 循环遍历该字符串的每一行,然后将每个 .charAt(index) 放入数组中的某个位置。记住二维数组是array[rows][cols] = string.charAt(x)

标签: java arrays string multidimensional-array maps


【解决方案1】:

你可以在Java中创建一个二维数组

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

例如,

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

将创建一个 2 行高和 3 列宽的数组。因此,您可以通过更改行让您的角色在网格上上下移动,并通过更改列让角色左右移动。

【讨论】:

    【解决方案2】:

    this 问题的答案正是您想要的,但使用整数。

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

    但我真的建议您使用一些使用对象的解决方案。由于您使用的是Java。

    我不知道你的项目,但我想那是一款基于瓷砖的 2D 游戏。您可以有一个 Tile 类,其中包含位置信息和图块的所有属性。

    考虑可扩展性和性能,可以使用另一种方法。

    【讨论】:

      【解决方案3】:

      你的字符串需要二维数组吗?为什么不存储像“ABCDEFGHI”这样的字符串并像访问 3x3 2D 数组一样访问它?

      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
      

      【讨论】:

      • 所以我一直在阅读这种方法,只是想澄清它是如何处理数组的。所以它在技术上只是一个二维数组?那么它似乎如何从二维数组中获取输出?感谢您的帮助
      • 您可以将 String 视为字符数组。这只是一些特定的数组。 :) 每个索引都来自范围 [0..n],其中 n 是该字符串中的字符数。具体索引 0
      • 哦好的,有道理,现在我可以看到它们是如何访问的了,谢谢你的帮助!
      猜你喜欢
      • 1970-01-01
      • 2021-04-04
      • 2019-01-21
      • 1970-01-01
      • 1970-01-01
      • 2016-08-10
      • 2013-10-10
      • 2010-12-19
      相关资源
      最近更新 更多