0

我有一个.txt具有以下格式数据的文件(坐标)

0   1     12.56
2   0     -56.2
1   2     78.2
0  -56.2  2
-2  8     0

我使用以下代码将此数据导入。

  public ArrayList<Point3d> loadFile(String filename) {
    ArrayList<Point3d> words = new ArrayList<Point3d>();
    try {
        Scanner chopper = new Scanner(new File(filename));
        while (chopper.hasNext()) {
            double x=chopper.nextDouble();
            double y=chopper.nextDouble();
            double z=chopper.nextDouble();
            Point3d p=new Point3d(x, y, z);
            words.add(p);
            // just calling nextLine will cause an exception at the end of the file unless you have an blank line there on purpose, so this makes sure it does
        }
        chopper.close();
    } catch (Exception e) {
       System.out.println(e);
    }
    return words;
}

导入工作正常。知道我想分开负坐标,正坐标我还想附加它们相应的索引值。

最后,我希望通过以下方式获得结果。

结果 :

 positiveList= {{0,0,1,12.56},{2,1,2,78.2}}

 negativeList={{1,2,0,-56.2},{3,0,-56.2,2},{4,-2,8,0}}

我怎样才能做到这一点。

4

3 回答 3

1

我的解决方案在这里使用数据结构,但如果您愿意Map,可以创建 2Lists个。Lists

while循环外添加:

Map<Integer, Point3D> negativeCoord = new HashMap<>();
Map<Integer, Point3D> positivetiveCoord = new HashMap<>();
int currentIndex = 0;

在里面:

Point3d p = new Point3d(x, y, z);
if(x < 0 || y < 0 || z < 0) {
    negativeCoord.put(currentIndex, p);
} else {
    positiveCoord.put(currentIndex, p);
}
currentIndex++;
于 2013-09-30T08:00:40.307 回答
0

这将是只有正面和负面的解决方案,而不知道它们的顺序。

public ArrayList<Point3D> getPositives(ArrayList<Point3D> points) {
    ArrayList<Point3D> positives = new ArrayList<>();
    for(Point3D next : points) {
        if(next.getX() >= 0 && next.getY() >= 0 && next.getZ() >= 0)
            posivites.add(next);
    }
    return positives.
}

public ArrayList<Point3D> getNegatives(ArrayList<Point3D> points) {
    ArrayList<Point3D> negatives = new ArrayList<>();
    for(Point3D next : points) {
        if(next.getX() < 0 || next.getY() < 0 || next.getZ() < 0)
            negatives.add(next);
    }
    return negatives.
}

根据您的情况,为信息创建一个自己的容器类可能是明智之举。

public class PointContainer {
    public final Point3D point;
    public final int order;
    public PointCoordinates (Point3D p, int order) {
        this.point = p;
        this.order = order;
    }
    public boolean isNegative() {
       return point.getX() < 0 || point.getY() < 0 || point.getZ() < 0;
    }
}

然后用它填充您的列表。

  public ArrayList<Point3d> loadFile(String filename) {
    ArrayList<PointContainer> words = new ArrayList<PointContainer>();
    try {
        Scanner chopper = new Scanner(new File(filename));
        for (int line = 0; chopper.hasNext(); line ++) {
            double x=chopper.nextDouble();
            double y=chopper.nextDouble();
            double z=chopper.nextDouble();
            Point3d p=new Point3d(x, y, z);
            PointCoordinates pc = new PointCoordinates(p, line);
            words.add(pc);
            // just calling nextLine will cause an exception at the end of the file unless you have an blank line there on purpose, so this makes sure it does
        }
        chopper.close();
    } catch (Exception e) {
       System.out.println(e);
    }
    return words;
}

这样,您就可以将这些信息用于您想做的所有事情。

于 2013-09-30T08:00:46.927 回答
0

您可以在加载文件后执行此操作,如下所示:

Map<Integer, Point3d> positiveList = new java.util.HashMap<Integer, Point3d>();
Map<Integer, Point3d> negativeList = new java.util.HashMap<Integer, Point3d>();
for (int i = 0; i < words.size(); i++) {
  Point3d p = words.get(i);
  if (p.x < 0 || p.y < 0 || p.z < 0) {
    negativeList.put(i + 1, p);
  } else {
    positiveList.put(i + 1, p);
  }
}

或将上述内容集成到您的 while 循环中,这样可以避免您对所有单词进行两次迭代。

于 2013-09-30T08:02:09.053 回答