Can't add elements to an arraylist?

Hi,
Although I wouldn’t say I’m a complete novice, I’ve been unsucessfully trying to do something quite simple : add objects to an ArrayList
Code is somethin like this :

ArrayList<Object> objects = new ArrayList<Object>();
//OR//
ArrayList<Object> objects = newArrayList();

void setup()
{
}

void draw()
{
   Object test = new Object();
   objects.add(test);
   println(objects.size());

  //OR//
  objects.add(new Object());
  println(objects.size());
}

The println always returns 0, and trying to use a foreach loop brings up nothing, there is no error message and the code compiles just fine. I feel like I’ve missed something very simple but I can’t figure out what it is

Thanks for your help!

1 Like

better ArrayList<Object> objects = new ArrayList();

thus you can tell the Arraylist its type

2 Likes

or explicit with class:

ArrayList<Obj> objects = new ArrayList<Obj>();

void setup() {
}

void draw() {
  objects.add(new Obj());
  if ( objects.size() > 100 ) objects.remove(0); // remove oldest
  println(objects.size());
}

class Obj{}
1 Like

Maybe your error is something else - but then show you constructor or class

1 Like

Ok, I think I have found the answer

ArrayList<Circle> circleArray = new ArrayList();

void draw()
{
  if(keyPressed)
  {
    Circle test = new Circle(mouseX, mouseY);
    circleArray.add(test);
  }
  println(circleArray.size());
  for(Circle circles : circleArray)
  {
    circles.display();
  }
}

The other class simply has a constructor that takes 2 floats x and y as inputs, and display() draws a circle with x and y and a fixed size.

It works just fine until I add “background(0);” at the beginning of my draw() method, after which nothing is added to my ArrayList (println returns 0.0), so I guess it has other properties

1 Like

And welcome to the forum!

2 Likes

full sketch


ArrayList<Circle> circleArray = new ArrayList();

void setup() {
  size(900, 600);
  background(0);
}//func

void draw() {
  background(0); 

  if (keyPressed||mousePressed) {
    Circle test = new Circle(mouseX, mouseY);
    circleArray.add(test);
  }

  println(circleArray.size());
  for (Circle circles : circleArray) {
    circles.display();
  }
}//func

// ====================================================================

class Circle {

  float x;
  float y;

  Circle(float x_, float y_) {
    x= x_;
    y= y_;
  }//constructor

  void display() {
    ellipse(x, y, 10, 10);
  }//method
  //
}//class
//
2 Likes

My Circle class was absolutely identical to yours besides mine using this.x and this.y instead of x and y for the display() method (not that I think it makes a difference)
However, while keyPressed didn’t work, using mousePressed || keyPressed fixed the issue, and after that, simply putting “keyPressed” was working again, so I’m confused

Here’s another one where it doesn’t work properly, it’s basically a graph you can put points on by simply clicking (graph is just there to draw the background and PrimeChecker checks whether a number’s polar coordinates are prime, they’re not used here)

//
ArrayList<Point> pointArray = new ArrayList();

int scale;
int numberOfPoints;
boolean checkPrime;
boolean placePoint = true;

void setup()
{
  numberOfPoints = 400;
  scale = 50;
  checkPrime = true;
  fullScreen();
}

void draw()
{
  background(0);
  translate(width/2, height/2);
  GraphDrawer graph = new GraphDrawer(scale);
  graph.circles();
  graph.graph();
  
  if(mousePressed || keyPressed)
  {
    Point test = new Point(mouseX, mouseY);
    pointArray.add(test);
    println(pointArray.size());
  }
  
  Point plotting = new Point(100, 100, 10);
  plotting.plotter(numberOfPoints, scale, checkPrime);
  
  for(Point points : pointArray)
    {
    
      points.display();
      println("test");
    }
}
//
class Point
{
  
  ArrayList<Point> pointArray = new ArrayList();
  
  float radius;
  float angle;
  float x;
  float y;
  int r;
  int numberOfPoints;
  int scale;
  boolean isPrime;
  boolean checkPrime;
  
  int pouet = 0;
  
  //Constructor for polar coordinates
  Point(float _radius, float _angle)
  {
    radius = _radius;
    angle = _angle;
  }
  
  //Constructor for carthesian coordinates
  Point(float _x, float _y, int _radius)
  {
    x = _x;
    y = _y;
    radius = _radius;
  }
  
  //Display for carthesian coordinates
  void display()
  {
   //Color and fill setup
   noStroke();   
   fill(255, 10, 10);
   //Plotting our point on the graph
   println(x);
   circle(x, y, radius);
  }

Thanks a lot for your help !

1 Like

As I said, I would always tell the arraylist its type

(which class the objects in the list come from)

2 Likes

You have this line as a duplicate in the class

Don’t.

2 Likes

Thanks again for your help, I fixed up a few things in my code, one of them being the one you pointed out, and it is working fine now!
As I suspected, it was a stupid mistake, but an important one nonetheless

1 Like