该文件如下所示:
2 3(2是数字或行数,3是数字 列)
10 2 1
30 4 3
所有数字都以空格分隔
import java.io.*;
import java.util.Scanner;
public class ReadingFileOfMatrices{
public static void main(String[]args) throws IOException{
Scanner keyboard=new Scanner (System.in);
File fileName= new File("m1.txt");
Scanner inputFile= new Scanner(fileName);
int row= inputFile.nextInt();
int column = inputFile.nextInt();
int [][] array=new int [row][column];
System.out.println("This array has "+row+" rows and "+column+" columns");
int firstLine=inputFile.nextInt();
for (int i=0;i<row; i++){
int number= inputFile.nextInt();
for (int j=0; j<column; j++){
int number1= inputFile.nextInt();
System.out.println(array[i][j]);
}
}
}
}
答案 0 :(得分:0)
首先从第一行读取row
和column
,然后剩下的数字是数组元素,使用嵌套循环,array[i][j]
引用的元素是{{1行和i
列
j