如何将具有不同值的相同对象生成到ArrayList中?

时间:2018-09-30 21:22:37

标签: java arraylist processing

我有一个cbx,我想将太阳系中的一些行星作为形状保存到这个ArrayList中,但是最后,由于其中存在许多对象,因此仅保存了最后一个对象参数全部。

这是主要内容:

ArrayList

这是ArrayList<Shape> shapes = new ArrayList<Shape>(); void setup() { size(1600, 800); generateSolarSystem(); } void draw() { update(); //background(255); int begin_x = 100; int begin_y = 100; int distance = 1; for (Shape s : shapes) { pushMatrix(); translate(begin_x+distance, begin_y); scale(1.1, 1.1); s.Draw(); text(s.name, begin_x+distance, begin_y+10); distance += 100; System.out.println("name: " + s.name); /*3*/ popMatrix(); } } void generateSolarSystem() { /**/ int d = 10; /**/ Shape planet = new Circle();; for(int idx = 0; idx<9; ++idx){ switch(idx) { case 0: //Mercury planet.planet_color_r = 128; planet.planet_color_g = 128; planet.planet_color_b = 128; planet.name = "Mercury"; planet.mass = "33011 x 10^23"; break; case 1: // Venus planet.planet_color_r = 255; planet.planet_color_g = 255; planet.planet_color_b = 0; planet.name = "Venus"; planet.mass = "4.8675 × 10^24 kg"; break; case 2: // Earth planet.planet_color_r = 0; planet.planet_color_g = 0; planet.planet_color_b = 255; planet.name = "Earth"; planet.mass = "4.8675 × 10^24 kg"; break; case 3: // Mars planet.planet_color_r = 255; planet.planet_color_g = 128; planet.planet_color_b = 0; planet.name = "Mars"; planet.mass = "4.8675 × 10^24 kg"; break; case 4: // Jupiter planet.planet_color_r = 150; planet.planet_color_g = 75; planet.planet_color_b = 0; planet.name = "Jupiter"; planet.mass = "4.8675 × 10^24 kg"; break; case 5: // Saturn planet.planet_color_r = 147; planet.planet_color_g = 131; planet.planet_color_b = 105; planet.name = "Saturn"; planet.mass = "4.8675 × 10^24 kg"; break; case 6: // Uranus planet.planet_color_r = 140; planet.planet_color_g = 205; planet.planet_color_b = 216; planet.name = "Uranus"; planet.mass = "4.8675 × 10^24 kg"; break; case 7: // Neptune planet.planet_color_r = 53; planet.planet_color_g = 110; planet.planet_color_b = 163; planet.name = "Neptune"; planet.mass = "4.8675 × 10^24 kg"; break; case 8: // Pluto planet.planet_color_r = 194; planet.planet_color_g = 196; planet.planet_color_b = 168; planet.name = "Pluto"; planet.mass = "4.8675 × 10^24 kg"; break; } shapes.add(planet); /*1*/ text("added: " + planet.name, 10, d); d += 10; /**/ } //check d += 10; for(Shape s : shapes) { /*2*/ text("check - " + s.name, 10, d); d += 10; } }

Shape

abstract class Shape { PVector position = new PVector(); PVector fill_color = new PVector(0, 0, 0); PVector stroke_color = new PVector(0, 0, 0); PVector select_fill_color = new PVector(255, 0, 0); PVector select_stroke_color = new PVector(255, 0, 0); Boolean selected = false; int planet_color_r; int planet_color_g; int planet_color_b; String name; String mass; int detailness = 10; abstract void Draw(); abstract Boolean Contains(int x, int y); } ,行星的名字看起来不错,但是在/*1*//*2*/,每个行星的名字都是“冥王星”。为什么?我该如何解决?

2 个答案:

答案 0 :(得分:1)

您将继续覆盖相同的planet对象。相反,您应该为循环中的每次迭代创建一个新对象:

for (int idx = 0; idx < 9; ++idx) {
    Shape planet = new Circle(); // Inside the loop!
    switch(idx) {

答案 1 :(得分:0)

现在,您只创建了一个Circle的实例。

然后,您将遍历行星索引,并将该索引中的字段设置为不同的值。这就是为什么只有循环的最后一次迭代看起来才被保存的原因。

要解决您的问题,您需要在循环的每次迭代中创建一个Circle的新实例。换句话说,切换这两行的顺序:

Shape planet = new Circle();
for(int idx = 0; idx<9; ++idx){