【发布时间】:2015-12-29 22:47:54
【问题描述】:
我有一段代码可以读取板(第一项)的数据(高度、宽度、行、列)和放置在板上的块(其余项目):
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class readFile {
private Scanner scanner;
public void openFile() {
try {
scanner = new Scanner(new
File("filePath.txt"));
}
catch (Exception e)
{
System.out.println("File not found");
}
}
public void readTheFile(){
while (scanner.hasNext()){
int height = scanner.nextInt();
int width = scanner.nextInt();
int row = scanner.nextInt();
int col = scanner.nextInt();
System.out.printf("%s %s %s %s\n", height, width,row,col);
}
}
public void closeFile(){
scanner.close();
}
}
这是输出:
5 4 0 0 //the dimensions of a board ; height, width, row, column
2 1 0 0 /*the rest are dimensions-heigh,width,row,column of blocks placed on
2 2 0 1 the board*/
2 1 0 3
2 1 2 0
1 2 2 1
1 1 3 1
1 1 3 2
1 1 4 0
1 1 4 3
我希望将其存储在 Arraylist 中并返回。请帮助
【问题讨论】:
-
创建一个代表每一行数据的 POJO,而不是打印结果,而是创建这个“行对象”的新实例并将其添加到
ArrayList
标签: java arrays arraylist input output