Java 将文本文件读入二维数组

Java 将文本文件读入二维数组,java,multidimensional-array,text-files,Java,Multidimensional Array,Text Files,我需要将一个文本文件读入2D数组,我可以将文件读入程序中,但我无法理解如何将它们读入2D数组。函数正在读取的数组是一个全局数组,因此它不在函数中 另外,我不知道数组一开始有多少行(目前设置为300行,因为它不会超过这个值),我知道这可能会导致问题,我看到一些人建议使用ArrayList,但是我必须有一个2D数组,所以我还想知道是否有办法将ArrayList更改为2D数组,这是否会更有效 public static String readMaze(String fileName) {

我需要将一个文本文件读入2D数组,我可以将文件读入程序中,但我无法理解如何将它们读入2D数组。函数正在读取的数组是一个全局数组,因此它不在函数中

另外,我不知道数组一开始有多少行(目前设置为300行,因为它不会超过这个值),我知道这可能会导致问题,我看到一些人建议使用ArrayList,但是我必须有一个2D数组,所以我还想知道是否有办法将ArrayList更改为2D数组,这是否会更有效

 public static String readMaze(String fileName) {
        String line = null;

        try {
            FileReader fileReader = new FileReader(fileName);

            BufferedReader bufferedReader = new BufferedReader(fileReader);

            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);

                for (int i = 0; i < mazeNew.length; i++) {

                    for (int j = 0; j < mazeNew[i].length; j++) {
                        // mazeNew[i][j] = ; - this is where I think something needs to be added
                    }
                }
            }

            bufferedReader.close();

        }

        catch (FileNotFoundException ex) {
            System.out.println("Unable to open file: " + fileName);
        }

        catch (IOException ex) {
            System.out.println("Error reading file: " + fileName);
        }

        return fileName;

    }

我不是java专家,但在PHP中,我会使用explode()。但我发现了一个例子,说明如何使用string.split()在java中执行同样的操作。结果是一样的。。。内容的二维数组。如果可能,您应该尝试向该文本文档中的行添加分隔符。但是您也可以拆分空格字符上的行

示例:

String foo = "This,that,other";
String[] split = foo.split(",");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < split.length; i++) {
    sb.append(split[i]);
    if (i != split.length - 1) {
        sb.append(" ");
    }
}
String joined = sb.toString();
String foo=“这个、那个、其他”;
字符串[]split=foo.split(“,”);
StringBuilder sb=新的StringBuilder();
对于(int i=0;i
我不是java专家,但在PHP中,我会使用explode()来完成。但我发现了一个例子,说明如何使用string.split()在java中执行同样的操作。结果是一样的。。。内容的二维数组。如果可能,您应该尝试向该文本文档中的行添加分隔符。但是您也可以拆分空格字符上的行

示例:

String foo = "This,that,other";
String[] split = foo.split(",");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < split.length; i++) {
    sb.append(split[i]);
    if (i != split.length - 1) {
        sb.append(" ");
    }
}
String joined = sb.toString();
String foo=“这个、那个、其他”;
字符串[]split=foo.split(“,”);
StringBuilder sb=新的StringBuilder();
对于(int i=0;i
如果您有固定的列数,您可以使用此选项,但请确保输入文件必须遵循相同的列数

FileReader fileReader = new FileReader(fileName);

        Scanner sc = new Scanner(fileReader);
        int row=0, col=0;
        while ((sc.hasNext()) != null) {
            if(col < colSize){   //colSize is size of column
                mazeNew[row][col]= sc.nextInt();
            }
            else{
                col=0;
                row++;
            }
        }
FileReader FileReader=新的FileReader(文件名);
Scanner sc=新扫描仪(文件阅读器);
int行=0,列=0;
while((sc.hasNext())!=null){
如果(col
如果您有固定的列数,您可以使用此选项,但请确保输入文件必须遵循相同的列数

FileReader fileReader = new FileReader(fileName);

        Scanner sc = new Scanner(fileReader);
        int row=0, col=0;
        while ((sc.hasNext()) != null) {
            if(col < colSize){   //colSize is size of column
                mazeNew[row][col]= sc.nextInt();
            }
            else{
                col=0;
                row++;
            }
        }
FileReader FileReader=新的FileReader(文件名);
Scanner sc=新扫描仪(文件阅读器);
int行=0,列=0;
while((sc.hasNext())!=null){
如果(col
这里有几个选项,但通常您会希望使用Java,因为它正是为这类事情而设计的。或者,使用现有的结构化数据格式(如JSON或XML)和现有的解析器,这样做的好处是,您可以使用大量的工具和库来处理这些格式,而无需重新发明任何东西

但是,按照扫描器方法,情况如下:

public static ArrayList<int[]> readMaze(String fileName) {

    // Number of ints per line:
    int width=2;

    // This will be the output - a list of rows, each with 'width' entries:
    ArrayList<int[]> results=new ArrayList<int[]>();

    String line = null;

    try {
        FileReader fileReader = new FileReader(fileName);

        BufferedReader bufferedReader = new BufferedReader(fileReader);

        Scanner mazeRunner = new Scanner(bufferedReader);

        // While we've got another line..
        while (mazeRunner.hasNextLine()) {

            // Setup current row:
            int[] row = new int[width];

            // For each number..
            for (int i = 0; i < width; i++) {

                // Read the number and add it to the current row:
                row[i] = mazeRunner.nextInt();

            }

            // Add the row to the results:
            results.add(row);

            // Go to the next line (optional, but helps deal with erroneous input files):
            if ( mazeRunner.hasNextLine() ) {

                // Go to the next line:
                mazeRunner.nextLine();

            }

        }

        mazeRunner.close();

    }

    catch (FileNotFoundException ex) {
        System.out.println("Unable to open file: " + fileName);
    }

    catch (IOException ex) {
        System.out.println("Error reading file: " + fileName);
    }

    return results;

}
publicstaticarraylistreadmaze(字符串文件名){
//每行的整数数:
整数宽度=2;
//这将是输出-一个行列表,每个行都有“宽度”条目:
ArrayList结果=新建ArrayList();
字符串行=null;
试一试{
FileReader FileReader=新的FileReader(文件名);
BufferedReader BufferedReader=新的BufferedReader(文件阅读器);
扫描器mazeRunner=新扫描器(bufferedReader);
//当我们有另一条线的时候。。
while(mazeRunner.hasNextLine()){
//设置当前行:
int[]行=新int[宽度];
//对于每个数字。。
对于(int i=0;i
这里有几个选项,但通常您会希望使用Java,因为它正是为这类事情而设计的。或者,使用现有的结构化数据格式(如JSON或XML)和现有的解析器,这样做的好处是,您可以使用大量的工具和库来处理这些格式,而无需重新发明任何东西

但是,按照扫描器方法,情况如下:

public static ArrayList<int[]> readMaze(String fileName) {

    // Number of ints per line:
    int width=2;

    // This will be the output - a list of rows, each with 'width' entries:
    ArrayList<int[]> results=new ArrayList<int[]>();

    String line = null;

    try {
        FileReader fileReader = new FileReader(fileName);

        BufferedReader bufferedReader = new BufferedReader(fileReader);

        Scanner mazeRunner = new Scanner(bufferedReader);

        // While we've got another line..
        while (mazeRunner.hasNextLine()) {

            // Setup current row:
            int[] row = new int[width];

            // For each number..
            for (int i = 0; i < width; i++) {

                // Read the number and add it to the current row:
                row[i] = mazeRunner.nextInt();

            }

            // Add the row to the results:
            results.add(row);

            // Go to the next line (optional, but helps deal with erroneous input files):
            if ( mazeRunner.hasNextLine() ) {

                // Go to the next line:
                mazeRunner.nextLine();

            }

        }

        mazeRunner.close();

    }

    catch (FileNotFoundException ex) {
        System.out.println("Unable to open file: " + fileName);
    }

    catch (IOException ex) {
        System.out.println("Error reading file: " + fileName);
    }

    return results;

}
publicstaticarraylistreadmaze(字符串文件名){
//每行的整数数:
整数宽度=2;
//这将是输出-一个行列表,每个行都有“宽度”条目:
ArrayList结果=新建ArrayList();
字符串行=null;
试一试{
FileReader FileReader=新的FileReader(文件名);
BufferedReader BufferedReader=新的BufferedReader(文件阅读器);
扫描器mazeRunner=新扫描器(bufferedReader);
//当我们有另一条线的时候。。
while(mazeRunner.hasNextLine()){
//设置当前行:
int[]行=新int[宽度];
//对于每个数字。。