我的任务是将两个矩阵写入一个文件(我已经完成),然后从文件中读取数据以执行各种计算。我遇到的困难是重建方法中的每个矩阵以执行计算。我决定使用ArrayList的ArrayList,以获得我需要的2-D矩阵。使用BufferedReader,我需要遍历一个矩阵,向一个arraylist添加元素,然后为第二个矩阵执行。我添加了矩阵上方的标题作为标记来查找我想要使用的矩阵数据。有更好的方法吗?
DATA
SITE DATE SAMPLE TAXA 1 2 3
NSV 8-Jul-13 Pool 1 10 10 10
NSV 8-Jul-13 Pool 2 10 10 10
NSV 8-Jul-13 Riffle 1 10 10 10
NSV 8-Jul-13 Riffle 2 10 10 10
NSV 23-Oct-13 Pool 1 10 10 10
NSV 23-Oct-13 Pool 2 10 10 10
NSV 23-Oct-13 Riffle 1 10 10 10
NSV 23-Oct-13 Riffle 2 10 10 10
SFP 4-Jul-13 Pool 1 10 10 10
SFP 4-Jul-13 Pool 2 10 10 10
SFP 4-Jul-13 Riffle 1 10 10 10
SFP 4-Jul-13 Riffle 2 10 10 10
SFP 27-Oct-13 Pool 1 10 10 10
SFP 27-Oct-13 Pool 2 10 10 10
SFP 27-Oct-13 Pool 100 10 10 10
SFP 27-Oct-13 Pool 150 10 10 10
SFP 27-Oct-13 Riffle 1 10 10 10
SFP 27-Oct-13 Riffle 2 10 10 10
SFP 27-Oct-13 Riffle 100 10 10 10
SFP 27-Oct-13 Riffle 150 10 10 10
RESULT
SITE DATE SAMPLE TAXA 1 2 3
NSV 8-Jul-13 Pool 1 10 10 10
NSV 8-Jul-13 Pool 2 10 10 10
NSV 8-Jul-13 Pool 100 0 0 0
NSV 8-Jul-13 Pool 150 0 0 0
NSV 8-Jul-13 Riffle 1 10 10 10
NSV 8-Jul-13 Riffle 2 10 10 10
NSV 8-Jul-13 Riffle 100 0 0 0
NSV 8-Jul-13 Riffle 150 0 0 0
NSV 23-Oct-13 Pool 1 10 10 10
NSV 23-Oct-13 Pool 2 10 10 10
NSV 23-Oct-13 Pool 100 0 0 0
NSV 23-Oct-13 Pool 150 0 0 0
NSV 23-Oct-13 Riffle 1 10 10 10
NSV 23-Oct-13 Riffle 2 10 10 10
NSV 23-Oct-13 Riffle 100 0 0 0
NSV 23-Oct-13 Riffle 150 0 0 0
SFP 4-Jul-13 Pool 1 10 10 10
SFP 4-Jul-13 Pool 2 10 10 10
SFP 4-Jul-13 Pool 100 0 0 0
SFP 4-Jul-13 Pool 150 0 0 0
SFP 4-Jul-13 Riffle 1 10 10 10
SFP 4-Jul-13 Riffle 2 10 10 10
SFP 4-Jul-13 Riffle 100 0 0 0
SFP 4-Jul-13 Riffle 150 0 0 0
SFP 27-Oct-13 Pool 1 10 10 10
SFP 27-Oct-13 Pool 2 10 10 10
SFP 27-Oct-13 Pool 100 10 10 10
SFP 27-Oct-13 Pool 150 10 10 10
SFP 27-Oct-13 Riffle 1 10 10 10
SFP 27-Oct-13 Riffle 2 10 10 10
SFP 27-Oct-13 Riffle 100 10 10 10
SFP 27-Oct-13 Riffle 150 10 10 10
文件数据的格式为:
public void sum() throws IOException {
ArrayList<ArrayList<Integer>> matrixArray = new ArrayList<>();
while(br.read() != -1) {
ArrayList row = new ArrayList();
//tried this, using two println spaces between matrices
//but realized can't use readline this way
while(!br.readLine().equals("\n\n")) {
row.add(br.read());
}
matrixArray.add(row);
}
- 更新 - 来自以下评论
Matrix One
A B
C D
Matrix Two
A B
C D
答案 0 :(得分:2)
您应该使用java.util.Scanner
课程。它更易于使用,并允许您逐行阅读。
一旦获得单独的行,您可以调用String#split
方法使用空格分隔数字。
这将为您提供一系列String
s。您现在可以轻松调用Integer.parseInt
方法从int
检索String
并将其添加到ArrayList
。
希望这有帮助。