java - 将 double 加载到 arrayList 中

标签 java

我有一个包含 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/

相关文章:

java - 8 拼图java : binary operator

java - 反序列化问题 - java.io.StreamCorruptedException : invalid type code: 00

java - 一个接一个地动画 View

java - 为什么我使用 try catch 时会出现错误

java - 为什么 Eclipse 将插件存储在不同的文件夹中以及如何修复它?

java - 这是 Java 中柯里化(Currying)的有效示例吗

java - 无法终止正在运行的应用程序

java - JAXB 为没有内容的标签设置一个值

java - 将持久化实体保留在事务之外

java - 二进制转字符矩阵帮助