Trouble expanding and writing to array of objects

your code should be runnable

also, the two codes do not match because in the 2nd code ant doesn’t appear

int antsPerNest = 10;
int antAmt = 0;
ant[] a;
CellXY cellXY=new CellXY(54, 166);

void setup() {
  size(660, 660);
  a = new ant[0];
  setType();
}

void draw() {
  for (ant a1 : a) {
    a1.display();
  }
}

void setType() {
  a = (ant[]) expand(a, antAmt + antsPerNest);
  for (int i = antAmt; i < a.length; i++) {
    cellXY=new CellXY(random(width), random(height));
    a[i] = new ant(cellXY.x, cellXY.y);
  }
  antAmt+= antsPerNest;
}

class CellXY {
  float x, y;

  //constr
  CellXY(float x, float y) {
    this.x = x;
    this.y = y;
  }
}
class ant {

  float x, y;

  //constr
  ant(float x, float y) {
    this.x = x;
    this.y = y;
  }

  void display() {
    //
    circle (x, y, 4);
  }
}

3 Likes