【问题标题】:String to 2 dimensional int array?字符串到二维int数组?
【发布时间】:2020-09-02 19:18:36
【问题描述】:

我有一个组合框,我正在使用一个动作监听器。

在该函数内部,如果选择了项目:1 或 2 等...,它会发现保存在数据库中的迷宫,它指向一个 Java 类。我把迷宫放在 maze_map 变量中。

这里 -> maze_map=connect.getMaze(which_one);

。 `

comboBox.addActionListener (new ActionListener () {
  public void actionPerformed(ActionEvent e) {
    int which_one=(int) comboBox.getSelectedItem();
    String maze_map = "";// map from database
    maze_map=connect.getMaze(which_one);
    System.out.println(which_one);
    System.out.println(maze_map);       
} });

好的,在我没事之前,我会在 maze_map 变量中取回一个字符串,其中包含 10x10 个数字。

包含 10x10 个数字的字符串迷宫地图。

1010000000
0111010000
0001010111
0000211011
0110001001
1110000010
0010108000
1110000061
1111001010
0100111001

但是,我需要将 maze_map 中的所有这些数字放入一个二维 int 数组中。 我的问题是怎么做?我正在尝试使用 parseint,但它不起作用。

【问题讨论】:

  • 欢迎来到 SO!需要更多详细信息并尝试自己解决此问题。请参阅How to Ask。谢谢!

标签: java arrays int dimensional


【解决方案1】:

创建一个 int 数组。

首先你需要将字符串数组分解成单独的字符串

str = "1010000000 0111010000";
String[] splited = str.split("\\s+");

然后遍历数组就可以使用了

int i=Integer.parseInt("currString"); 

将每个 String 转换为 int。

然后只需将 int 添加到 int 数组中。

【讨论】:

  • 但这会创建一维数组。 OP 想要一个二维数组。
猜你喜欢
  • 1970-01-01
  • 2018-08-22
  • 2021-06-12
  • 2019-09-14
  • 1970-01-01
  • 2020-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多