【问题标题】:How to generate same object with different values into an ArrayList?如何将具有不同值的相同对象生成到 ArrayList 中?
【发布时间】:2018-09-30 21:22:37
【问题描述】:

我有一个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*//*3*/ 每个行星的名称都是“冥王星”。为什么?我该如何解决这个问题?

【问题讨论】:

  • 尝试将 Shape planet = new Circle();; 移动到 for 循环中,删除双精度 ; ,而不是 ++idx 尝试使用 idx++

标签: java arraylist processing


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    要解决您的问题,您需要在循环的每次迭代中创建一个 Circle 的新实例。也就是说,把这两行的顺序调换一下:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-22
      • 2020-09-18
      • 1970-01-01
      相关资源
      最近更新 更多