我需要实现的是从包含两列双精度数的文本文件中导入数据,如下所示:
201.0176 1.06E+03
201.7557 1.11E+01
202.0201 2.02E+02
202.2064 9.76E+00
202.8342 1.11E+01
203.0161 2.02E+02
203.1638 9.61E+00
203.3843 1.13E+01
这些数据多达50,000行。我希望每一列被导入到一个单独的数组,但我不能找出如何识别单独的列之间。我尝试了以下操作:
public class CodeTests {
public static ArrayList assignArray(String filePath) throws FileNotFoundException {
Scanner s = new Scanner(new File(filePath));
ArrayList<Double> list = new ArrayList<Double>();
while (s.hasNext()){
list.add(s.nextDouble());
}
s.close();
return list;
}
public static void main(String[] args) throws IOException {
/*
*
*/
ArrayList arrayMZ;
arrayMZ = assignArray("F:/Stuff/Work/Work from GSK/From Leeds/ja_CIDFragmenter/testFile.txt");
for(int i = 0; i < arrayMZ.size(); i++)
System.out.println(arrayMZ.get(i));
System.out.println("End");
}
}
从运行这个我得到输出:
run:
201.0176
1060.0
201.7557
11.1
202.0201
202.0
202.2064
9.76
202.8342
11.1
203.0161
202.0
203.1638
9.61
203.3843
11.3
End
BUILD SUCCESSFUL (total time: 1 second)
谁能帮我把这些列分成两个数组,甚至分成一个单独的二维数组,在数组[0]的列下,其中有第一列数据,数组[1]中有第二列数据。例如:
[0] [1]
201.0176 1.06E+03
201.7557 1.11E+01
202.0201 2.02E+02
202.2064 9.76E+00
202.8342 1.11E+01
203.0161 2.02E+02
203.1638 9.61E+00
203.3843 1.13E+01
我已经尽我所能把这件事说清楚了,但如果还有什么问题,请告诉我。
谢谢
你可以这样做:
public static ArrayList[] assignArray(String filePath) throws FileNotFoundException {
Scanner s = new Scanner(new File(filePath));
ArrayList[] list = new ArrayList[2];
ArrayList<Double> col1 = new ArrayList<Double>();
ArrayList<Double> col2 = new ArrayList<Double>();
while (s.hasNext()){
String[] data = s.nextLine().split("\s+");
col1.add(Double.parseDouble(data[0]));
col2.add(Double.parseDouble(data[1]));
}
s.close();
list[0] = col1;
list[1] = col2;
return list;
}
类似于
for(int i = 0; i < arrayMZ.size() / 2; i+=2)
System.out.printf("%.4ft%En", arrayMZ.get(i), arrayMZ.get(i + 1));