我有一个包含 double 列表的文本文件。该行的第一个值是 x 值,第二个值是 y 值。
103.0 274.0
133.0 383.0
342.0 250.0
204.0 126.0
177.0 357.0
...
如何读取这些值并将它们加载到数组列表中?
ArrayList<Point> store = new ArrayList<Point>();
File file = fc.getSelectedFile();
StringBuilder all = new StringBuilder();
BufferedReader reader = new BufferedReader( new FileReader(file));
String input = null;
while ((input = reader.readLine()) != null)
{
String a = all.append(input+"\n").toString();
String[] hold = a.split(" ");
double x = Double.parseDouble(hold[0]);
int aa = (int)(x);
double y = Double.parseDouble(hold[1]);
int bb = (int)(y);
store.add(new Point(aa, bb));
}
最佳答案
1: Get an input stream of the file.
2: read line by line using readLine() method
3: split the string by SPACE using split() method of String class
4: the String array you got has two elements now.
5: Double.parseDouble(array[0]) and Double.parseDouble(array[1]) are value of x and y
6: store these values into the corresponding values of Point object
7: add that point object to ArrayList.
8: Done
关于java - 将 double 加载到 arrayList 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9765265/