java - JAVA填充二维数组

标签 java arrays file 2d

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

文件

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

//看起来你的帖子主要是代码;请添加更多详细信息。所以我添加了一些无意义的文字。看起来你的帖子主要是代码;请添加更多详细信息。所以我添加了一些无意义的文字。

最佳答案

你可以像这样填充二维数组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';
        }
    }
}

关于java - JAVA填充二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59047459/

相关文章:

java - mongodb scala 驱动程序 casbah 是否自动管理连接池

java - HttpURLConnection.getResponseCode 未获取 HTTP 响应代码 206

java - 对对象数组进行二分查找以查找元素的字段

c++ - 将文件的元素加载到 C++ 中的二维数组中

java - 安卓应用程序: How do I create a directory in the internal storage

c++ - 将缓冲区写入文件(存储 1585 字节而不是 1580)

java - 为什么我的整数不是 0,而是我在倒计时器中设置的值?

java - 策略模式: How to make classes with constructors as enums?

php - 在 CodeIgniter 的 MySql WHERE 子句中使用数组

java - 如何迭代文本文件中的路径列表