簡體   English   中英

在JAVA中填充2D數組

[英]Fill 2D array in JAVA

我有一個文件,其中有數字,必須將其插入到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

//看起來您的帖子大部分是代碼; 請添加更多詳細信息。 所以我要添加廢話。 看起來您的帖子大部分是代碼; 請添加更多詳細信息。 所以我要添加廢話。

您可以像這樣填充2d數組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';
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM