如何找出协调点是否为负数

时间:2013-09-30 07:52:32

标签: java

我有一个.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}}

我该怎么办呢。

3 个答案:

答案 0 :(得分:1)

此处的解决方案使用Map数据结构,但如果您愿意,可以创建2 Lists 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++;

答案 1 :(得分: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;
}

通过这种方式,您可以将这些信息用于您想要做的所有事情。

答案 2 :(得分: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循环中,这样可以避免迭代所有单词两次。