我正在尝试从文本文件中获取2D数组。到目前为止,我访问了文件并获得了文件中的所有数字,但是所有这些数字都是字符串,因此我使用了[[split()然后将其转换为双精度。如何显示这些数字,就像文件一样
1.65 4.50 2.36 7.45 3.44 6.232.22 -3.24 -1.66 -5.48 3.46
4.23 2.29 5.29
2.76 3.76 4.29 5.48 3.43
3.38 3.65 3.76
2.46 3.34 2.38 8.26 5.34
这是我到目前为止的内容:
public static void main(String[] a) throws FileNotFoundException {
File file = new File("district3.txt");
Scanner scan = new Scanner(file);
String b;
String[] c;
int r = 6;
double[][]arr = new double[r][];
while(scan.hasNextLine()) {
//get number as String
b = scan.nextLine();
//split them
c = b.split(" ");
for(String i:c)
System.out.println(Double.parseDouble(i) );
}
}