java - 在 ArrayList 中获取 "size() does not exist"错误

标签 java arraylist size boids

我正在修改一个项目的植绒模拟。添加对象没问题,但是当我尝试删除对象时,我在第 240 行收到“函数 size() 不存在”错误。问题可能出在 AdjSize() 和 subBoid() 中,但我不知道是什么原因造成的。我已经稍微简化了代码,所以它只是 ArrayList 植绒的东西。

Flock flock;
float k, l;
int previous = 0;
int test = 0;

void setup() 
{
  size(1920, 1080);
  flock = new Flock();
  for (int i = 0; i < 150; i++) 
  {
    flock.addBoid(new Boid(width/2,height/2));
  }
}

void draw()
{
  background(0);
  flock.run();
  flock.AdjSize(); 
}



// The Boid class
class Boid 
{

  PVector location;
  PVector velocity;
  PVector acceleration;
  float r;
  float maxforce;  // Maximum steering force
  float maxspeed;  // Maximum speed

  Boid(float x, float y)
  {
    acceleration = new PVector(0, 0);

    float angle = random(TWO_PI);
    velocity = new PVector(cos(angle), sin(angle));

    location = new PVector(x, y);
    r = 2.0;
    maxspeed = 2;
    maxforce = 0.03;
  }



  void run(ArrayList<Boid> boids)
  {
    flock(boids);
    update();
    borders();
    render();
  }

  void applyForce(PVector force)
  {
    acceleration.add(force);
  }

  void flock(ArrayList<Boid> boids)
  {
    PVector sep = separate(boids);   // Separation
    PVector ali = align(boids);    // Alignment
    PVector coh = cohesion(boids);   // Cohesion
    sep.mult(1.5);
    ali.mult(1.0);
    coh.mult(1.0);
    applyForce(sep);
    applyForce(ali);
    applyForce(coh);
  }

  // Method to update location
  void update()
  {
    // Update velocity
    velocity.add(acceleration);
    // Limit speed
    velocity.limit(maxspeed);
    location.add(velocity);
    // Reset accelertion to 0 each cycle
    acceleration.mult(0);
  }

  PVector seek(PVector target)
  {
    PVector desired = PVector.sub(target, location); 
    desired.normalize();
    desired.mult(maxspeed);

    PVector steer = PVector.sub(desired, velocity);
    steer.limit(maxforce);  // Limit to maximum steering force
    return steer;
  }

  void render()
  {
    float theta = velocity.heading2D() + radians(90);

    fill(255, 255,255);
    stroke(255);
    pushMatrix();
    translate(location.x, location.y);
    rotate(theta);
    beginShape();
    vertex(r*15, r*17); //Left wing tip
    vertex(r*25, r*10); //Left wing top point
    vertex(r*30, r*13); //middle divit
    vertex(r*35, r*10); //right wing top point
    vertex(r*45, r*17); //right wing tip
    vertex(r*30, r*13); //underswoop
    endShape(CLOSE);
    popMatrix();
  }

  void borders()
  {
    if (location.x < -r) location.x = width+r;
    if (location.y < -r) location.y = height+r;
    if (location.x > width+r) location.x = -r;
    if (location.y > height+r) location.y = -r;
  }

  PVector separate (ArrayList<Boid> boids) {
  float desiredseparation = 25.0f;
  PVector steer = new PVector(0, 0, 0);
  int count = 0;
  for (Boid other : boids)
  {
    float d = PVector.dist(location, other.location);
    if ((d > 0) && (d < desiredseparation))
    {
      PVector diff = PVector.sub(location, other.location);
      diff.normalize();
      diff.div(d);      // Weight by distance
      steer.add(diff);
      count++;          // Keep track of how many
    }
  }
  if (count > 0) 
  {
    steer.div((float)count);
  }
  if (steer.mag() > 0) 
  {
    steer.normalize();
    steer.mult(maxspeed);
    steer.sub(velocity);
    steer.limit(maxforce);
  }
    return steer;
  }

  // Alignment
  PVector align (ArrayList<Boid> boids)
  {
    float neighbordist = 50;
    PVector sum = new PVector(0, 0);
    int count = 0;
    for (Boid other : boids)
    {
      float d = PVector.dist(location, other.location);
      if ((d > 0) && (d < neighbordist))
      {
        sum.add(other.velocity);
        count++;
      }
    }
    if (count > 0)
    {
      sum.div((float)count);
      sum.normalize();
      sum.mult(maxspeed);
      PVector steer = PVector.sub(sum, velocity);
      steer.limit(maxforce);
      return steer;
    }
    else
    {
      return new PVector(0, 0);
    }
  }

  PVector cohesion (ArrayList<Boid> boids) {
  float neighbordist = 50;
  PVector sum = new PVector(0, 0);   
  int count = 0;
  for (Boid other : boids)
  {
    float d = PVector.dist(location, other.location);
    if ((d > 0) && (d < neighbordist))
    {
        sum.add(other.location); // Add location
      count++;
    }
  }
  if (count > 0)
  {
      sum.div(count);
    return seek(sum);  // Steer towards the location
  }


  else
  {
      return new PVector(0, 0);
  }
 }
}
class Flock
{
  ArrayList<Boid> boids; // An ArrayList for all the boids

  Flock()
  {
    boids = new ArrayList<Boid>(); // Initialize the ArrayList
  }

  void run()
  {
    for (Boid b : boids)
    {
      b.run(boids); 
    }
  }

  void AdjSize()
  {
    if(test == 0)
    {
      flock.addBoid(new Boid(width/2,height/2));
      test = 1;
    }
    else
    {
    flock.subBoid(flock.size() - 1);
    }
  }

  void addBoid(Boid b)
  {
    boids.add(b);
  }

  void subBoid(Boid b)
  {
   boids.remove(b);
  }
}

最佳答案

您是在 Flock 对象上调用 size(),而不是在 ArrayList 上。如果你想让 Flock 有一个可调用的 size() 方法,你必须先给它一个。例如,

class Flock {
   // ....

  public int size() {
    return boids.size();
  }
}

关于java - 在 ArrayList 中获取 "size() does not exist"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21177382/

相关文章:

java - 成功实现了线程,但我不知道如何将结果写入文件

java - 如何在 LinkedHashMap 的特定索引/位置添加元素?

java - 方法结束时事务未提交

java - 如何提高 Java ArrayList 的性能

java - Android:为敌人创建存储阵列

java - 将 Maven 与 Java 项目一起使用是否会减少 war 的大小?

java - 使用 JVMTI 进行死锁检测

spring-mvc - Spring 缓存中的错误?为什么list.addAll会对结果产生影响?

python - Python 中的矩阵大小

c - 如果我知道变量的地址,变量类型的字节大小的目的是什么?