Loading...
Logo
Processing Forum

how to use ArrayList?

in General Discussion  •  Other  •  1 year ago  

The explanation of ArrayList is the segment of code:  http://processing.org/reference/ArrayList.html

It lacks the class. I don't understand. Could somebody complete it so as to make it be better understood?

Thank you sooooooooo much

Replies(5)

I think that explenation is quite clear but ok, i will do a attempt.

If you use a normall array you must give it a size:

Copy code
  1. PVector[] vectors = new PVector[10];

For a ArrayList you just declare the type it holds.

Copy code
  1. ArrayList<PVector> vectors = new ArrayList<PVector>();

to add a item use

Copy code
  1. vectors.add(new PVector(10, 10, 10));

you can loop over like:

Copy code
  1. for(int i = 0; i < vectors.size(); i++) {

  2. }

notice that it uses size instead of length.

to get a item use

Copy code
  1. PVector v = vectos.get(i);

hope that helps

Thx but i still have problem using  drawings.get(i) = drawing.get(i-1) . here's what i write:
Copy code
  1. ArrayList <Drawing> drawings = new ArrayList <Drawing>();

  2. void setup(){
  3. size(400,400);
  4. background(255);
  5. colorMode(HSB);
  6. }

  7. void draw(){}

  8. void mouseDragged(){
  9.   
  10. drawings.add(new Drawing(mouseX,mouseY));

  11. for(int i = drawings.size()-1;i>0;i--){
  12. drawings.get(i) = drawing.get(i-1);
  13. }

  14. for(int i=0;i<drawings.size;i++){
  15. fill(c,100);
  16. drawings.get(i).display();}
  17. }

  18. class Drawing{
  19.   float x,y,r;
  20.   color c;

  21.   Drawing(float ax,float ay){
  22.   x=as;
  23.   y=ay;
  24.   r=random(2,20);
  25.   c=color(random(100,200),255,255);
  26.   }  

  27.   void display(){
  28.   fill(c,100);
  29.   ellipse(drawing[i],r,r);}

  30. }
i want to make a drawing pen. what's wrong?
U can create a class for that:

something like this: 

class Ball
{      
      float x, y, size;

      Ball ( float _x, float _y, float _sz)
      {
            x = _x;
            y = _y;
            size = _sz

       }
      
      void display()
      {
            ellipse(x, y, size,size);
      }
      
      void move()
      {
      //code for moving

      }

      boolean finished()
      {
           // test for out of window
            return false; // left this as a placeholder, so balls are never finished ... never removed ...
      }
}


This won't move nothing but should already compile... (untested)

What seemed strange for me first time i saw an ArrayList is this creating an object for each iteration and the casting stuff. in this part (see comments):

for (int i = balls.size()-1; i > 0; i--) { 

    Ball ball = (Ball) balls.get(i); // for each iteration creates a temp Ball object
                                     // and assign to it the Ball in the array at i
                                     // ball (temp obj) = balls.get(i); (the Ball in your ArrayList)
                                     // the (Ball) is the casting.see note below
    ball.move(); // cal stuff using temp obj
    ball.display();
    if (ball.finished()) {
      // Items can be deleted with remove().
      balls.remove(i); 
      

    }
// here this temp is "discarded" 
      // and another one will be created for the next iteration
}

I think this example is old from when Processing could not use generics, (<type>) if u do like clankil3r sad 
ArrayList<Ball> balls = new ArrayList<Ball>();
U don't need to cast as you told that this array is holding Balls, so:
Ball ball = balls.get(i);
should work and, i think, not sure, even the more like array syntax also:
balls.get(i).move();
Works?


drawings.get(i) = drawing.get(i-1);

The = sign means that this is an assignment statement. The assignment statement says evaluate the epression on the right hand side and store the result in the variable on the left hand side.

drawings.get(i)  is not a varaible it is an object of type drawing.

It is not clear what you are hoping to achieve and there are some other errors. Anyway I have modified your code so try it out and see  if it is the effect you want.

Copy code
  1. ArrayList <Drawing> drawings = new ArrayList <Drawing>();

  2. void setup() {
  3.   size(400, 400);
  4.   background(255);
  5.   colorMode(HSB);
  6. }

  7. void draw() {
  8.   background(255);
  9.   for (int i=0;i<drawings.size();i++) {
  10.     drawings.get(i).display();
  11.   }
  12. }

  13. void mouseDragged() {
  14.   drawings.add(new Drawing(mouseX, mouseY));
  15. }

  16. class Drawing {
  17.   float x, y, r;
  18.   color c;

  19.   Drawing(float ax, float ay) {
  20.     x=ax;
  21.     y=ay;
  22.     r=random(2, 20);
  23.     c=color(random(100, 200), 255, 255);
  24.   }

  25.   void display() {
  26.     fill(c, 100);
  27.     ellipse(x,y, r, r);
  28.   }
  29. }




Copy code
  1. for(int i = drawings.size()-1;i>0;i--){
  2. drawings.get(i) = drawing.get(i-1);
  3. }
If you try to shift the objects, you can use drawings.remove(0);
After the drawings.add(...), it will keep the list at a constant size, while removing the oldest elements each time you add a new one.