使用字符串数组在 Java 中创建二维(地图)数组
Using an array of Strings to create a 2D (map) array in Java
如何在 Java 中创建二维字符数组?我正在研究这是否适用于我的地图目标,你可以在地图上移动角色,但没有发现任何有用的东西。我以前在 C++ 中做过这个(在一点帮助下),但不知道如何在 Java.
对于 C++ 版本,我从一维字符串数组开始:
"#######.#.~~~####.##.......~~~..##.~~....H....######..#######",
"#####..T.#~~~..##.H......~~~...#..T.~~.........######.###....",
"######..~~~~#######...#..~~~.........~~~.##.###..#####.###...",
"...###..~~~.######...##H.~~~.@........~~.#.####...##.H.###...",
".#####..~~.~~###C.....#..~~~.....#...~~~..###..#....##....#..",
"######....~~~.~.##....#..~~~.....#....~~~.#######C...........",
"#######.H..~.~~~~#....#..~~~.....###...~~~.##########........",
"########.H...~~~~~.....~~~......H..##....~~~.H######...T...##",
(你是@符号)
然后能够使用 2 个嵌套的 for 循环将每个字符串分解成单独的字符,将其分解成基本上是一个二维数组,您可以在其中移动角色。
这是一个很好的方法吗?如果是的话,怎么做? (我现在已经花了 10 个小时试图让一个基本版本工作)。有更好的方法吗?我想稍后用这样的地图创建一个非常复杂的游戏,但需要帮助想出如何做。
您可以通过
在Java中创建一个二维数组
char[][] arr = new char[number of rows][number of columns];
例如,
char[][] arr = new char[2][3];
将创建一个 2 行高 3 列宽的数组。因此,您可以通过改变行让角色在网格中上下移动,通过改变列让角色左右移动。
this 问题的答案正是您想要的,但是是整数。
char[][] multi = new char[number of lines][number of columns];
但我真的建议您改用一些使用对象的解决方案。由于您使用的是 Java.
我不知道你的项目,但我想那是一个基于图块的 2D 游戏。您可以拥有一个 Tile class,其中包含位置信息和图块的所有属性。
可以使用另一种方法来考虑可伸缩性和性能。
您的字符串需要二维数组吗?为什么不像 "ABCDEFGHI" 那样存储字符串并像访问 3x3 二维数组一样访问它?
将 x,y "coordinates" 映射到 index。
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
如何在 Java 中创建二维字符数组?我正在研究这是否适用于我的地图目标,你可以在地图上移动角色,但没有发现任何有用的东西。我以前在 C++ 中做过这个(在一点帮助下),但不知道如何在 Java.
对于 C++ 版本,我从一维字符串数组开始:
"#######.#.~~~####.##.......~~~..##.~~....H....######..#######",
"#####..T.#~~~..##.H......~~~...#..T.~~.........######.###....",
"######..~~~~#######...#..~~~.........~~~.##.###..#####.###...",
"...###..~~~.######...##H.~~~.@........~~.#.####...##.H.###...",
".#####..~~.~~###C.....#..~~~.....#...~~~..###..#....##....#..",
"######....~~~.~.##....#..~~~.....#....~~~.#######C...........",
"#######.H..~.~~~~#....#..~~~.....###...~~~.##########........",
"########.H...~~~~~.....~~~......H..##....~~~.H######...T...##",
(你是@符号) 然后能够使用 2 个嵌套的 for 循环将每个字符串分解成单独的字符,将其分解成基本上是一个二维数组,您可以在其中移动角色。
这是一个很好的方法吗?如果是的话,怎么做? (我现在已经花了 10 个小时试图让一个基本版本工作)。有更好的方法吗?我想稍后用这样的地图创建一个非常复杂的游戏,但需要帮助想出如何做。
您可以通过
在Java中创建一个二维数组char[][] arr = new char[number of rows][number of columns];
例如,
char[][] arr = new char[2][3];
将创建一个 2 行高 3 列宽的数组。因此,您可以通过改变行让角色在网格中上下移动,通过改变列让角色左右移动。
this 问题的答案正是您想要的,但是是整数。
char[][] multi = new char[number of lines][number of columns];
但我真的建议您改用一些使用对象的解决方案。由于您使用的是 Java.
我不知道你的项目,但我想那是一个基于图块的 2D 游戏。您可以拥有一个 Tile class,其中包含位置信息和图块的所有属性。
可以使用另一种方法来考虑可伸缩性和性能。
您的字符串需要二维数组吗?为什么不像 "ABCDEFGHI" 那样存储字符串并像访问 3x3 二维数组一样访问它?
将 x,y "coordinates" 映射到 index。
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