【问题标题】:Fill 2D array in JAVA用JAVA填充二维数组
【发布时间】:2019-11-26 09:17:01
【问题描述】:

我有一个文件,其中包含必须插入二维数组的数字,但它不起作用 这是我的代码 抱歉英语不好

文件

2,1 //starting point
3,2 //ending point
5,6 //array size which is maze
0,1,1,1,1,1 //from here till end its all gonna be inserted to 2d array
0,1,0,0,1,1 //and i somehow managed to take starting, ending point and arraysize 
0,1,0,0,1,0 //all i have to do is fill the array 
0,0,1,0,1,0
0,0,1,1,1,0

完整代码

public class Mouse {
// global variables to take input from file
    public static int startRow;
    public static int startCol;
    public static int endRow;
    public static int endCol;
    public static int arrayRow;
    public static int arrayCol;
    public static String arrayDetail;
public static void main(String[] args) throws IOException {

       StringBuilder stringbuilder = new StringBuilder(); 
     try {
           BufferedReader input = new java.io.BufferedReader(new java.io.FileReader("src/input.txt"));
           List<String> lines = new ArrayList<String>();
           String[] stringArray = new String[lines.size()];
           String line = null; 
            while ((line = input.readLine()) != null) {
                lines = Arrays.asList(line.split("\\s*,\\s*"));
                stringArray = lines.toArray(stringArray);
              for (int i = 0; i < stringArray.length; i++) {
                  stringbuilder.append(stringArray[i]);
               }
            }
  }
     catch (FileNotFoundException e) {
          System.err.println("File not found.");
      }
     System.out.println(stringbuilder);
     //giving global variables a value that i need
     startRow =     Integer.parseInt(stringbuilder.substring(0, 1));
     startCol =     Integer.parseInt(stringbuilder.substring(1, 2));
     endRow =       Integer.parseInt(stringbuilder.substring(2, 3));
     endCol =       Integer.parseInt(stringbuilder.substring(3, 4));
     arrayRow =     Integer.parseInt(stringbuilder.substring(4, 5));
     arrayCol =     Integer.parseInt(stringbuilder.substring(5, 6));
     arrayDetail =                       stringbuilder.substring(6);
    //i cant give array values to the long variable because it's out of range


        Home home = new Home();
        home.print(); // print maze before releasing mouse
        if (home.walk(startRow,startCol)) { //starting position
            System.out.println("Ok"); //print okay if its successful
        }
        else {
        System.out.println("No Solution"); //if unsuccessful
        }
        home.print(); // print maze to track how mouse went

    }

}
class Home{
    Home(){}
    int[][] A = new int[Mouse.arrayRow][Mouse.arrayCol]; //giving array size
    {
        for (int i = 0; i < A.length; i++) {
            for (int j = 0; j < A[i].length; j++) {
                for (int k = 0; k < Mouse.arrayDetail.length(); k++) {
                        //and its the problem doesn't take values
                     A[i][j] = Integer.parseInt(Mouse.arrayDetail.substring(k, k+1));
                      //and it gives me bunch of 000 it means its null and not taking values right ?
                }
            }
        }
    }
  // road is 1
  // wall is 0
  // the roads that mouse went only one time is 2
  // 3 is the road that mouse went but did not get success i mean the roads mouse went 2 times
    public boolean walk(int row, int col) { //method to walk
        boolean result = false;
        if (check(row, col)){
            A[row][col] = 3;
            if (row == Mouse.endRow && col == Mouse.endCol) { //ending position
                result = true;
            }
            else {
                result = walk(row+1, col);  //down
            if(!result)
                result = walk(row, col+1);  //right
            if(!result)
                result = walk(row-1, col);  //up
            if(!result)
                result = walk(row, col-1);  //left
        }
    }
        if (result == true) {
            A[row][col] = 2;
        }
        return result;
    }
    public boolean check(int row, int col) { //check there is a road
        boolean result = false;
        if (row<A.length && row >=0 && col >=0 && col < A[0].length ) {
        if (A[row][col] == 1) {
            result = true;
        }
    }
        return result;
}
    public void print() { //method to print maze
        for (int i = 0; i < A.length; i++) {
            for (int j = 0; j < A[i].length; j++) {
                System.out.print(A[i][j]);
            }
            System.out.println();
        }
    }
}

输出

000000
000000
000000
000000
000000
No Solution
000000
000000
000000
000000
000000

// 看起来你的帖子主要是代码;请添加更多细节。所以我添加了无意义的文本。看起来您的帖子主要是代码;请添加更多细节。所以我添加了无意义的文本。

【问题讨论】:

  • 究竟是什么不起作用?你能给我们一个错误吗?
  • 请提供更多关于代码应该做什么的上下文
  • 它没有显示任何错误。它只是不填充数组。
  • 当我打印数组时它显示一堆 000
  • 你能告诉我们文件的内容吗?

标签: java arrays file 2d


【解决方案1】:

你可以像这样填充二维数组A

int[][] A = new int[Mouse.arrayRow][Mouse.arrayCol]; //giving array size
{
    for (int i = 0; i < A.length; i++) {
        for (int j = 0; j < A[i].length; j++) {
            A[i][j] = Mouse.arrayDetail.charAt(i * Mouse.arrayCol + j) - '0';
        }
    }
}

【讨论】:

  • 哇,它的工作原理非常感谢你。但是 arrayDetail 是一个 String 你是如何将它转换为 int 类型的?你能给我解释一下吗?
  • arrayDetail 是字符串,但 arrayDetail.charAt 返回一个字符,在 java 中,您可以在算术运算中使用字符,如 +、- 并将结果直接分配给 int,而无需强制转换或解析。
  • 数组从 0 开始对吗?但它从 1 开始,但它仍然有效,非常感谢你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-05
  • 1970-01-01
  • 1970-01-01
  • 2017-04-17
  • 1970-01-01
  • 2019-02-01
  • 2016-05-27
相关资源
最近更新 更多