我想創建點的平面與此代碼:無法創建點的平面
類環境:
public class Environment {
public ArrayList<Point> plane = new ArrayList<Point>();
public void addToPlane(Point point) {
plane.add(point);
}
public void showplane() {
for(int x=1; x<=2500; x++) {
Point point2 = new Point();
point2 = plane.get(x);
System.out.println("x = "+point2.getX()+"; y = "+point2.getY());
}
}
}
類EnvironmentTest:
public class EnvironmentTest {
public static void main(String[] args) {
Environment env = new Environment();
Helper help = new Helper();
help.createPlane(env,50,50);
env.showplane();
}
}
助手類別:
public class Helper {
public void createPlane(Environment env, int i, int j) {
Point point = new Point();
for(int x=0; x<=i; x++) {
for(int y=0; y<=j; y++) {
point.setLocation(x, y);
System.out.println(x+"+"+y);
env.addToPlane(point);
}
}
}
}
我得到了控制檯什麼,當我運行showplane()是
...
50+38
50+39
50+40
50+41
...
一切都很好,現在,但是當我嘗試列出我的觀點我只得到:
x = 50.0; y = 50.0
x = 50.0; y = 50.0
x = 50.0; y = 50.0
x = 50.0; y = 50.0
x = 50.0; y = 50.0
我在哪裏犯錯?
您正在使用一個'Point',所以'plane'的全部內容將引用單個Point,它將具有最後一個座標。 – blm