Java:将txt文件读入2D数组

Java:将txt文件读入2D数组,java,arrays,map,arraylist,2d,Java,Arrays,Map,Arraylist,2d,对于家庭作业,我们必须读入一个包含地图的txt文件。有了地图,我们应该读入它的内容,并将它们放入一个二维数组中 我已经设法将该文件读入一维字符串数组列表,但我遇到的问题是如何将其转换为二维字符数组 到目前为止,我在构造器中看到的是: try{ Scanner file=new Scanner (new File(filename)); while(file.hasNextLine()){ ArrayList<String> lines= new Ar

对于家庭作业,我们必须读入一个包含地图的txt文件。有了地图,我们应该读入它的内容,并将它们放入一个二维数组中

我已经设法将该文件读入一维字符串数组列表,但我遇到的问题是如何将其转换为二维字符数组

到目前为止,我在构造器中看到的是:

try{

  Scanner file=new Scanner (new File(filename));

    while(file.hasNextLine()){

        ArrayList<String> lines= new ArrayList<String>();

        String line= file.nextLine();

        lines.add(line);    

        map=new char[lines.size()][];

    }
}
catch (IOException e){
    System.out.println("IOException");
}
试试看{
扫描仪文件=新扫描仪(新文件(文件名));
while(file.hasNextLine()){
ArrayList行=新的ArrayList();
String line=file.nextLine();
行。添加(行);
map=newchar[lines.size()][];
}
}
捕获(IOE异常){
System.out.println(“IOException”);
}
当我打印line.size()时,它打印出1,但当我查看文件时,它有10


提前感谢。

您必须在循环之外创建列表。在实际实现中,为每一行创建一个新列表,因此它的大小始终为1

// ...
Scanner file = new Scanner(new File(filename));
List<String> lines = new ArrayList<String>();  // <- declare lines as List
while(file.hasNextLine()) {
// ...
/。。。
扫描仪文件=新扫描仪(新文件(文件名));

列表行=新建ArrayList();// 您必须在循环之外创建列表。在实际实现中,为每一行创建一个新列表,因此它的大小始终为1

// ...
Scanner file = new Scanner(new File(filename));
List<String> lines = new ArrayList<String>();  // <- declare lines as List
while(file.hasNextLine()) {
// ...
/。。。
扫描仪文件=新扫描仪(新文件(文件名));

列表行=新建ArrayList();// 更改代码如下:

public static void main(String[] args) {
        char[][] map = null;
        try {
            Scanner file = new Scanner(new File("textfile.txt"));
            ArrayList<String> lines = new ArrayList<String>();
            while (file.hasNextLine()) {
                String line = file.nextLine();
                lines.add(line);
            }
            map = new char[lines.size()][];
        } catch (IOException e) {
            System.out.println("IOException");
        }
        System.out.println(map.length);
    }
publicstaticvoidmain(字符串[]args){
char[]map=null;
试一试{
扫描仪文件=新扫描仪(新文件(“textfile.txt”);
ArrayList行=新的ArrayList();
while(file.hasNextLine()){
String line=file.nextLine();
行。添加(行);
}
map=newchar[lines.size()][];
}捕获(IOE异常){
System.out.println(“IOException”);
}
System.out.println(映射长度);
}

更改代码如下:

public static void main(String[] args) {
        char[][] map = null;
        try {
            Scanner file = new Scanner(new File("textfile.txt"));
            ArrayList<String> lines = new ArrayList<String>();
            while (file.hasNextLine()) {
                String line = file.nextLine();
                lines.add(line);
            }
            map = new char[lines.size()][];
        } catch (IOException e) {
            System.out.println("IOException");
        }
        System.out.println(map.length);
    }
publicstaticvoidmain(字符串[]args){
char[]map=null;
试一试{
扫描仪文件=新扫描仪(新文件(“textfile.txt”);
ArrayList行=新的ArrayList();
while(file.hasNextLine()){
String line=file.nextLine();
行。添加(行);
}
map=newchar[lines.size()][];
}捕获(IOE异常){
System.out.println(“IOException”);
}
System.out.println(映射长度);
}