如何通过java中的点类读取多个点?

时间:2013-05-28 06:15:38

标签: java

public class point3d {
float x;
float y;
float z;  
public point3d(float x, float y, float z){
   this.x = x;
   this.y = y;
   this.z = z;
}
public point3d(){
    x = 0;
    y = 0;
    z = 0;
}
public float getX(){
    return x;
}
void setX(float x) {
    this.x =x;
}
public float getY(){
    return y;
}
void setY(float y) {
    this.y =y;
} 
public float getZ(){
    return z;
}         
void setZ(float z) {
    this.z = z;
}
public String toString()
{
    return "(" + x + ", " + y + "," + z + ")";
}        
}

这是我编写的point3d类代码,我想通过这个point3d类读取多个点,这在主类中给出了如何实现这一点。请帮助我?

2 个答案:

答案 0 :(得分:3)

首先,类应该根据Naming Conventions以大写字母开头。

其次,您应该在主要类中为Point3d创建一个容器,例如List

接下来,您可以迭代它并执行您的逻辑。

List<Point3d> points = new ArrayList<>(); // this is JDK7
List<Point3d> points = new ArrayList<Point3d>(); // this is before JDK7, pick one

points.add(new Point(4F, 3F, 2F)); // let's create some points to iterate over
points.add(new Point(23F, 7F, 5F));

for(Point3d point : points) {
    // do some logic with point
}

后续问题是,您希望通过这些points实现什么?

答案 1 :(得分:0)

我想我现在明白你的问题。你想制作一个三维矩形,这意味着你需要4个点(除非你想要它也有深度,在这种情况下你需要8个)。

所以你需要的是一个包含4个point3d类的数组:

point3d[] rectangle = new point3d[4];

然后你只需要分配给那个数组:

rectangle[0] = new point3d(x,y,z); //note that the first element is 0, not 1
rectangle[1] = new point3d(x2,y2,z2);
...

如果您想稍后访问它们:

System.out.println(rectangle[0].getX());

我建议你做一些阅读:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html