ArrayLists

Now you know how to use objects and create your own classes. You know how to use arrays to create variables that hold multiple values, and you know how to create arrays of objects.


This is a companion discussion topic for the original entry at https://happycoding.io/tutorials/processing/arraylists
1 Like

hello i have a question in all the example in this tutorial i notice that you created an arraylist with a class then the object but when it came to using the variable you create a new object(?still not good with terminology) and give it the values of the first arraylist

ArrayList<PVector> circles = new ArrayList<PVector>();

  for(int i = 0; i < circles.size(); i++) {
   PVector circle = circles.get(i);
   ellipse(circle.x, circle.y, 50, 50);
  }
}

why do you not just use circles directly and why if i try to use the arraylist dircetly dose it come up with |the global variable “x” does not exist| but is fixed when i create a new object within the loop

also sorry for any wording issues and thanks for happy coding its very great

If I understand your question, you’re asking why the above code contains this line:

PVector circle = circles.get(i);

The honest answer is I did this to try to make it more clear that circles.get(i) returns a PVector instance. But note that it’s not creating a new instance, it’s storing the existing instance in a new variable. You could also do this:

for(int i = 0; i < circles.size(); i++) {
  ellipse(circles.get(i).x, circles.get(i).y, 50, 50);
}

More realistically, I’d probably use an enhanced for like this:

for(Circle circle : circles) {
  ellipse(circle.x, circle.y, 50, 50);
}

For your error, can you post the code you’re trying to run?

this is just the beginnings i know its messy and incomplete so sorry

ArrayList<Ball> Koala = new ArrayList<Ball>(5);

void setup(){
  size(100,100);
  for(int i = 0; i<= Koala.size(); i++){
    Koala.add(new Ball(width/2,height/2,300,0));

  }
}
void draw(){
  for(int i = 0; i<= Koala.size(); i++){
    Koala.get(i);
    ellipse(Dot.BallX,Dot.BallY,Dot.BallS,Dot.BallS);
  }
}

void mousePressed(){
  for(int i = 0; i<= Koala.size(); i++){
    Koala.get(i);
    if(mouseX <= Koala.BallX && mouseX <= Koala.BallX || !(mouseY <= Koala.BallX) && !(mouseX <= Koala.BallY)){
    }   
  }
}

class Ball{
  float BallX;
  float BallY;
  float BallS;
  int Layer;
  
  Ball(float BallX, float BallY, float BallS, int Layer){
    this.BallX = BallX;
    this.BallY = BallY;
    this.BallS = BallS;
    this.Layer = Layer;
  }
  
}
1 Like

When I run your code, I get an error on this line:

ellipse(Dot.BallX, Dot.BallY, Dot.BallS, Dot.BallS);
The variable "Dot" does not exist

That error message tells me that the code tries to use a variable named Dot, but no variable with that name was ever created.

So I look at the surrounding code:

void draw() {
  for (int i = 0; i<= Koala.size(); i++) {
    Koala.get(i);
    ellipse(Dot.BallX, Dot.BallY, Dot.BallS, Dot.BallS);
  }
}

And indeed, I don’t see a Dot variable that was created in this scope (either inside this function or at the top of the sketch).

I’m guessing that you meant to get the Ball instance out of your ArrayList like this:

void draw() {
  for (int i = 0; i<= Koala.size(); i++) {
    Ball Dot = Koala.get(i);
    ellipse(Dot.BallX, Dot.BallY, Dot.BallS, Dot.BallS);
  }
}

After I do that, I get a different error:

Type mismatch: cannot convert from Object to Ball

This is because you need the <> type (also called generics) in your ArrayList definition:

ArrayList<Ball> Koala = new ArrayList(5);

And if I fix that, I get a different error, but it’s very similar to your first issue so I’m betting that you can fix that one yourself.

Try that out, and if you’re still banging your head against it after 15 minutes, let me know and I’ll try to walk you through it!

Thanks again for posting. I know that asking questions can be scary, but it’s the only way we learn!

i’m sorry i made an error in what i posted this was the code

the dot thing was from an incomplete update trying to resolve the issue it works but why do i get the global variable error?

thank you for your responses

ArrayList<Ball> Koala = new ArrayList<Ball>(5);

void setup(){
  size(100,100);
  for(int i = 0; i<= Koala.size(); i++){
    Koala.add(new Ball(width/2,height/2,300,0));

  }
}
void draw(){
  for(int i = 0; i<= Koala.size(); i++){
    Koala.get(i);
    ellipse(Koala.BallX,Koala.BallY,Koala.BallS,Koala.BallS);
  }
}

void mousePressed(){
  for(int i = 0; i<= Koala.size(); i++){
    Koala.get(i);
    if(mouseX <= Koala.BallX && mouseX <= Koala.BallX || !(mouseY <= Koala.BallX) && !(mouseX <= Koala.BallY)){
    }   
  }
}

class Ball{
  float BallX;
  float BallY;
  float BallS;
  int Layer;
  
  Ball(float BallX, float BallY, float BallS, int Layer){
    this.BallX = BallX;
    this.BallY = BallY;
    this.BallS = BallS;
    this.Layer = Layer;
  }
  
}

Your latest code still has the same issue here:

void draw() {
  for (int i = 0; i<= Koala.size(); i++) {
    Koala.get(i);
    ellipse(Koala.BallX, Koala.BallY, Koala.BallS, Koala.BallS);
  }
}

The error message is a little confusing (The global variable "BallX does not exist), but it’s happening because the Koala variable is the whole ArrayList, but BallX is only valid if you’re using a Ball instance. You probably want to do something like this:

void draw() {
  for (int i = 0; i<= Koala.size(); i++) {
    Ball ball = Koala.get(i);
    ellipse(ball.BallX, ball.BallY, ball.BallS, ball.BallS);
  }
}

Now this code uses a variable named ball to hold the instance returned from Koala.get(i), which means that BallX, BallY, and BallS are valid.

You have a similar issue in your mousePressed() method as well.

why doesn’t .get(i) get an instance?

It does, but then you have to use that reference. One way to do that is to store it in a variable. Here’s another approach you could take:

void draw() {
  for (int i = 0; i<= Koala.size(); i++) {
    ellipse(Koala.get(i).BallX, Koala.get(i).BallY, Koala.get(i).BallS, Koala.get(i).BallS);
  }
}

Or you could use an enhanced for since you don’t really care about the i variable:

void draw() {
  for (Ball ball : Koala) {
    ellipse(ball.BallX, ball.BallY, ball.BallS, ball.BallS);
  }
}

ah! that explains it. thank you

1 Like