ArrayList- Please help me
I want to use array List. I have an example from arrayList. But in this example they use class. But they don’t write the code that is related to it. I should make class to run this program.
But I cannot make class.
// It won't compile because it's missing the Ball class.
ArrayList balls;
void setup() {
size(200, 200);
balls = new ArrayList(); // Create an empty ArrayList
balls.add(new Ball(width/2, 0, 48)); // Start by adding one element
}
void draw() {
background(255);
// With an array, we say balls.length, with an ArrayList,
// we say balls.size(). The length of an ArrayList is dynamic.
// Notice how we are looping through the ArrayList backwards, this
// is because we are deleting elements from the list
for (int i = balls.size()-1; i >= 0; i--) {
// An ArrayList doesn't know what it is storing so we have
// to cast the object coming out
Ball ball = (Ball) balls.get(i);
ball.move();
ball.display();
if (ball.finished()) {
// Items can be deleted with remove()
balls.remove(i);
}
}
}
void mousePressed() {
// A new ball object is added to the ArrayList, by default to the end
balls.add(new Ball(mouseX, mouseY, ballWidth));
}