-1

我正在尝试逐行读取文本文件。每行有两个用逗号分隔的双数(代表 x,y 点)。我想把它们放在两个双精度数组中(一个包含 x 点,另一个包含 y 点),以便它们在文件中。数组的大小应与文件中的点数(即文件中的行数)相同。

这些文件可能有不同的点数,所以我不想为所有文件声明一个固定大小。我该怎么做呢?

编辑

我这样做的方式有问题:

BufferedReader input; 

ArrayList<Double> xpointArrayList = new ArrayList<Double>();
ArrayList<Double> ypointArrayList = new ArrayList<Double>();
try {
    input = new BufferedReader(new FileReader(args[0]));
    String line;
    while ((line = input.readLine()) != null) {
        line = input.readLine();
        String[] splitLine = line.split(",");

        double xValue = Double.parseDouble(splitLine[0]);
        double yValue = Double.parseDouble(splitLine[1]);

        xpointArrayList.add(xValue);
        ypointArrayList.add(yValue);
    }
    input.close();

    } catch (IOException e) {

    } catch (NullPointerException npe) {

    }

    double[] xpoints = new double[xpointArrayList.size()];
    for (int i = 0; i < xpoints.length; i++) {
        xpoints[i] = xpointArrayList.get(i);
    }
    double[] ypoints = new double[ypointArrayList.size()];
    for (int i = 0; i < ypoints.length; i++) {
        ypoints[i] = ypointArrayList.get(i);
    }

当我对 xpoints 和 ypoints 数组执行 Array.toSring 调用时。它只有一个数字。例如在文件中:

1,2 / 3,4 / 0,5

xpoints 数组只有 3.0,ypoints 数组只有 4.0。它哪里出错了?

4

4 回答 4

1

只做一个ArrayListtype怎么样Point。一个ArrayList会增长到内容的大小,所以它应该像做这样的事情(伪代码)一样简单......

ArrayList<Point> points = new ArrayList<Point>();

for (each line of the file){
    String line = file.readLine();
    String[] splitLine = line.split(",");

    double xValue = Double.parseDouble(splitLine[0]);
    double yValue = Double.parseDouble(splitLine[1]);

    points.add(new Point(xValue,yValue));
}

该类Point被设计为 2 个double值的容器,用于表示任何抽象点,它应该完全适合您的需要。请参阅文档。

如果你真的需要使用 2 个数组,我的建议是使用一个ArrayList<Double>...

ArrayList<Double> xPoints = new ArrayList<Double>();
ArrayList<Double> yPoints = new ArrayList<Double>();

for (each line of the file){
    String line = file.readLine();
    String[] splitLine = line.split(",");

    double xValue = Double.parseDouble(splitLine[0]);
    double yValue = Double.parseDouble(splitLine[1]);

    xPoints.add(xValue);
    yPoints.add(yValue);
}

有关这方面的更多信息,请参阅ArrayList文档。

如果你真的需要使用数组,你可以ArrayList像这样将其转换为数组......

Double[] xArray = (Double[])xPoints.toArray();
Double[] yArray = (Double[])yPoints.toArray();
于 2012-12-06T01:37:20.060 回答
1

不。您要求一个可变大小的数组。使用 ArrayList。您不必指定初始大小,它会增长到您需要的大小。此外,创建一个点类来处理这些数据可能是值得的。这样你就可以得到一个点列表。

public class Point
{
private double x;
private double y;

public point(double x, double y)
{
this.x = x;
this.y = y;
}

public double getX()
{
return x;
}

public double getY()
{
return y;
}
}

List<Point> myPoints = new ArrayList<Point>();

而且,如果您真的很喜欢常规数组: myPoints.toArray();

于 2012-12-06T01:37:59.140 回答
1

正如您已经打开并从文件中读取的简单方法是将计数器放入循环并读取所有行。

  int count =0;
  while ((strLine = br.readLine()) != null)   {
      count++;
      System.out.println( strLine + " : "+ count);
  }
于 2012-12-06T01:40:39.687 回答
0

FileObject.length() 将返回文件中的字节数,将其除以双精度类型整数(8 字节?)的大小,您将拥有存储在文件中的双精度变量数,将该数字除以 2再次(或将您的原始数字除以 4),如果我正确理解您的问题,您应该在文件中有行数。

于 2012-12-06T01:33:12.700 回答