繁体   English   中英

如何用文本文件中的字符串填充二维数组/

[英]How to fill a 2D Array with stings from a text file/

我必须使用以下文本文件构建矩阵/网格。

.0...00...
.0F0......
..0..0....
.0.0.....0
..........
..........
....0.0.0.
.0.0......
0........0
S.0.....00

我使用的是二维数组,但在用上述字符串填充数组时遇到问题。 我该怎么做才能用这些字符串填充二维数组?

public static void readFile() throws IOException {
String txtFile = "path to txt file";

        int rows = 0;
        int cols = 0;
    
        //get no. of rows
        Scanner s = new Scanner(new File(txtFile));
        while (s.hasNextLine()){
            s.nextLine();
            rows++;
        }
    
        //get no. of columns
        BufferedReader r = new BufferedReader(new FileReader(txtFile));
        List<String> list = new ArrayList<String>();
        String getCol;
        while((getCol = r.readLine()) != null){
            list.add(getCol);
        }
        cols = list.size();
    
       //initialize array
        String[][] grid = new String[rows][cols];
        for(int i =0; i < grid.length; i++){
            for(int j =0; j < grid.length; j++){
                    if(s.hasNext()){
                        grid[i][j] = s.next();
                    }
            }
        }
    
    }

你不需要那么混乱。 您使用 Scanner 来获取文件中的行数,那么为什么不使用它来获取其他所有内容呢? 一旦你有行数就关闭文件然后再次打开它:

Scanner s = new Scanner(new File(txtFile));
int rows = 0;
while (s.hasNextLine()){
    s.nextLine();
    rows++;
}
s.close();

因为它是一个方形矩阵(行数与每行中的列数相同),您现在可以声明和初始化矩阵 (2D) 数组:

String[][] matrix = new String[rows][rows];

现在只需使用相同的扫描仪 object 重新打开文件并实际用数据填充矩阵。 请记住....Java 中的二维数组是 Arrays 的数组。

s = new Scanner(new File(txtFile));
int rowCounter = 0;
while (s.hasNextLine()){
    matrix[rowCounter] = s.nextLine().trim().split("");
    rowCounter++;
}
s.close();

注意矩阵(二维数组)是如何填充的。 因为二维数组是 Arrays 的数组,所以您只需要知道该行要保存的数组。 代码:

s.nextLine().trim().split("");

将读取的行拆分为您需要的 String[] 数组。 所以这条线:

matrix[rowCounter] = s.nextLine().trim().split("");

与以下内容基本相同:

String[] lineArray = s.nextLine().trim().split("");
matrix[rowCounter] = lineArray;

integer 变量rowCounter (从 0 开始)指定的行中的matrix二维数组现在包含当前读取行的 String[] 数组。 现在rowCounter变量递增 1: rowCounter++; 准备处理下一个文件行并将其添加到二维数组中。

整个事情可能看起来像:

try {
    //get no. of rows/columns
    Scanner s = new Scanner(new File(txtFile));
    int rows = 0;
    while (s.hasNextLine()){
        s.nextLine();
        rows++;
    }
    s.close();
        
    String[][] matrix = new String[rows][rows];
        
    s = new Scanner(new File(txtFile));
    int rowCounter = 0;
    while (s.hasNextLine()){
        matrix[rowCounter] = s.nextLine().trim().split("");
        rowCounter++;
    }
    s.close();
        
    // Print the Matrix (2D Array)...
    for (String[] strg : matrix) {
        for (String str : strg) {
            System.out.print(str);
        }
        System.out.println();
    }
}
catch (FileNotFoundException ex) {
    ex.printStackTrace();
}

并且控制台 Window 应该显示:

.0...00...
.0F0......
..0..0....
.0.0.....0
..........
..........
....0.0.0.
.0.0......
0........0
S.0.....00

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM