我需要实现的是从包含两列双精度的文本文件中导入数据,如下所示:
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)
任何人都可以帮助我将这些列分成两个数组,甚至分成一个2D数组,在array [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
我试图尽可能清楚地说明这一点,但如果还有其他事情请告诉我。
由于
答案 0 :(得分:1)
你可以这样做:
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;
}
并获取包含数据的两个ArrayList数组。
答案 1 :(得分:0)
类似的东西:
for(int i = 0; i < arrayMZ.size() / 2; i+=2)
System.out.printf("%.4f\t%E\n", arrayMZ.get(i), arrayMZ.get(i + 1));