首页 > 解决方案 > 用JAVA填充二维数组

问题描述

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

文件

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

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

标签: javaarraysfile2d

解决方案


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

推荐阅读