java - 从文件Java中获取矩阵
问题描述
我目前有一个格式为的文本文件
matrix
row
a
b
c
row
d
e
f
row
g
h
i
row
j
k
l
matrix
row
m
n
o
p
q
row
r
s
t
u
v
我想将其转换为两个整数矩阵(存储为 2 个二维数组),格式为
a b c
d e f
g h i
j k l
和
m n o p q
r s t u v
到目前为止,我已经创建了文件的 Scanner 对象并将每一行放入一个文本数组中:
Scanner sf = new Scanner(new File("C:\\textfiles\\matrices.txt"));
int maxIndex = -1;
String text[] = new String[10000]; // I added more than necessary for safety
while (sf.hasNext()){
maxIndex++;
text[maxIndex] = sf.nextLine();
}
sf.close();
这样,文本文件现在包含在一个字符串数组中,其中每一行都是数组的一个新元素。现在,我想将数组分成两个数组,每个数组都是矩阵。我应该如何继续?(注意:我是一个完全的初学者并且希望得到简单的答案(没有数组列表、哈希图等,这就是为什么这个问题不是如何从 java 中的 txt 文件中读取两个矩阵的重复,因为它使用了 BufferedReader,并且还有其他潜在的重复问题,所以我想澄清一下)
我目前在顶部之后拥有的东西:
int counter = 0;
int iCounter = 0; // row
int jCounter = 0; // column
int matrix1[][];
int matrix2[][];
while (counter < maxIndex){
if (counter = 0)
{
\\not yet written...
}
\\not yet written...
}
解决方案
这可以满足您的需求。不幸的是,使用 2D 数组执行此操作要困难得多,因为一旦设置了数组的大小,就很难管理更改它。因此使用起来ArrayList
要容易得多。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Main {
public static final String MATRIX = "matrix";
public static final String ROW = "row";
public static void main(String[] args) throws FileNotFoundException {
// Use correct file name here
Scanner sf = new Scanner(new File("matrices.txt"));
// This is a List of 2D Lists
List<List<List<String>>> matrices = new ArrayList<>();
// easier to process lines as we're reading them in so we
// only iterate over the file once
while (sf.hasNext()) {
boolean hasBeenProcessed = false;
String inputValue = sf.nextLine();
switch (inputValue) {
case MATRIX:
ArrayList<List<String>> matrix = new ArrayList<>();
matrices.add(matrix);
hasBeenProcessed = true;
break;
case ROW:
List<List<String>> currentMatrix = getMatrixBeingProcessed(matrices);
currentMatrix.add(new ArrayList<String>());
hasBeenProcessed = true;
break;
}
if (!hasBeenProcessed) {
List<List<String>> currentMatrix = getMatrixBeingProcessed(matrices);
List<String> currentRow = getCurrentRow(currentMatrix);
currentRow.add(inputValue);
}
}
// Print out the results:
int i = 1;
for (List<List<String>> matrix : matrices) {
System.out.println("Matrix " + i);
for (List<String> row : matrix) {
for (String element : row) {
System.out.print(element + " "); // no newline until end of the row
}
System.out.println(); // new line
}
i++;
System.out.println(); // new line
}
}
private static List<String> getCurrentRow(List<List<String>> currentMatrix) {
int lastRow = currentMatrix.size() - 1;
return currentMatrix.get(lastRow);
}
private static List<List<String>> getMatrixBeingProcessed(List<List<List<String>>> matrices) {
int lastMatrix = matrices.size() - 1;
List<List<String>> currentMatrix = matrices.get(lastMatrix);
return currentMatrix;
}
}
输出:
Matrix 1
a b c
d e f
g h i
j k l
Matrix 2
m n o p q
r s t u v
Process finished with exit code 0
推荐阅读
- wpf - 将 DataGrid 行数显示为控件并正确更新
- c - 函数的参数是指向声明为 const 的参数的指针,如何处理?
- settings - 如何将 Light Table 配置为始终显示 Workspace
- android - Media Recorder 示例应用程序崩溃 - 为什么会这样?
- grafana - 在 InfluxQL 中计算值和(移动)平均值之间的差异
- python - 如何在 Python 3x 中以垂直和水平方式格式化列表?
- python-3.x - 尝试从 FourSquare API 调用所有素食餐厅
- r - R:计算长/纬度和形状文件之间距离的较少计算密集型方法
- python - Python/Selenium 在未找到时尝试对元素使用“if”语句
- python - 验证并处理错误以将有意义的状态传递回客户端