[英]How to read int numbers (matrix) from file.txt and add to array?
I want to multiply two matrices.我想将两个矩阵相乘。 I have 2 files with 2 different integers (matrices).
我有 2 个带有 2 个不同整数(矩阵)的文件。
file1.txt
4 3 4 6
-1 10 4 -1
4 7 2 -8
file2.txt
3 0 0
0 3 0
0 0 3
0 2 4
How can I read these files separately into a two-dimensional array, so that it is convenient to multiply.怎样才能把这些文件分别读成一个二维数组,方便相乘。 I've a code where the size of the matrix is indicated at the beginning, but what if there could be different size of matrices?
我有一个代码,其中在开头指示了矩阵的大小,但是如果矩阵的大小可能不同怎么办? Here is my code with given size:
这是我给定大小的代码:
public static void main(String[] args) throws IOException {
try {
Scanner input = new Scanner(new File(file1.txt));
int m = 3; // I need the size for random matrix
int n = 5; // I need the size for random matrix
int[][] a = new int[m][n];
while (input.hasNextLine()) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
try{
a[i][j] = input.nextInt();
System.out.println("number is "+ a[i][j]);
}
catch (java.util.NoSuchElementException e) {
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
It would be better to implement a separate method reading a file into matrix if the dimensions of the matrix are known:如果矩阵的维度已知,最好实现一个单独的方法将文件读入矩阵:
public static int[][] readFileWithMatrix(String filename, int rows, int cols) throws Exception {
int[][] arr = new int[rows][cols];
try (Scanner input = new Scanner(new File(filename))) { // use try-with-resources to close Scanner
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
arr[i][j] = input.nextInt();
}
}
}
return arr;
}
Then it would be simpler to use this method:那么使用这种方法会更简单:
int[][] arr3x4 = readFileWithMatrix("file1.txt", 3, 4);
int[][] arr4x3 = readFileWithMatrix("file2.txt", 4, 3);
//... do matrix multiplication
Here try the following code which takes all the input from file assuming that it contains only the matrix elements and store it into a dynamic 2D array a
eg, list of list.在这里尝试下面的代码,假设它只包含矩阵元素并将其存储到一个动态二维数组中
a
例如,列表列表,它从文件中获取所有输入。 First I read the first line of the file and take out all the numbers from it which gives me the total number of columns of the matrix.首先,我读取文件的第一行并从中取出所有数字,这给了我矩阵的总列数。 Next I keep on reading the input lines until the end of file which expands the row of the matrix
a
接下来,我继续阅读输入行,直到文件末尾扩展矩阵的行
a
public static void main(String[] args) throws IOException {
try {
Scanner input = new Scanner(new File(file1.txt));
List<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();
int row=0;
String cols[] = input.nextLine().split(" ");
a.add(row, new ArrayList<Integer>());
for (int j = 0; j < cols.length; j++) {
try {
a.get(row).add(Integer.parseInt(cols[j]));
} catch (Exception e) {
}
}
row++;
while (input.hasNextLine()) {
a.add(row, new ArrayList<Integer>());
for (int j = 0; j < cols.length; j++) {
try {
a.get(row).add(input.nextInt());
} catch (java.util.NoSuchElementException e) {
}
}
row++;
}
System.out.println("Rows: "+row+" Columns: "+cols.length);
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a.get(i).size(); j++) {
System.out.println("Number is "+ a.get(i).get(j));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Demo code in here: https://ideone.com/ZEcgPR#stdin演示代码在这里: https://ideone.com/ZEcgPR#stdin
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.