ArrayList problem

edited January 2017 in Programming Questions

Im tring to make an ArrayList for object and i was struggling. I looked it up and i saw people using

        ArrayList <object> object = new ArrayList <object>();
        void draw() {
        for (int i=0; i<object.**size**();i++) {
        object.**get**(i).draw();
        }
        }

people said that these are functions that ArrayList uses that Array doesnt. when i tried this it didn't work and said that "the function get(int) does not exist". i want to make an ArrayList that adds 12 of the same object.

Answers

  • @cloudi --

    1. Don't call your class "object" -- give it a meaningful name, and Capitalize it!
    2. Don't give your list instance the exact same name as the class it is based on. Here are my pets, a list made out of several Cat objects:

      ArrayList<Cat> myPets = new Arraylist<Cat>.
      
    3. Try starting with the working example of an ArrayList of objects from the reference documentation:

    Notice that it uses a capitalized class ("Particle") and a different list name ("particles").

  • edited January 2017

    @jeremydouglass says

    Don't give your list instance the exact same name as the class it is based on.

    But the entire truth is, that is exactly what is causing the problem.

  • class name must not be the same name as your ArrayList

    Class Name in Capitals

    one object small letters, singular

    ArrayList, small letters plural

    ArrayList<Ball> balls;
    
    int xPos = 10;
    int yPos = 0;
    int rectWidth = 10; //  width-20;
    int rectHeight = 20 ;  // 120;
    int beginPosition = 0;
    int stopPosition = 150;
    int mouseVar;
    float rCol, gCol, bCol;
    
    void setup() {
      size(400, 800);
      frameRate(10);
      smooth();
      noStroke();
      balls = new ArrayList();
    } // func 
    
    void draw() {
      background(255);
      // for-loop
      for (int i = balls.size()-1; i >= 0; i--) { 
        Ball ball = balls.get(i);
        ball.moveThis();
        ball.display();
      } // for 
      // a new for-loop to delete balls
      // (it is better to have 2 separate for-loops)
      for (int i = balls.size()-1; i >= 0; i--) {
        Ball ball = balls.get(i);
        if (ball.y+ball.h > height) {
          balls.remove(i);
        } // if
      } // for 
      println(balls.size());
    } // func 
    
    void mouseReleased() {
      for (int i = 0; i < balls.size(); i++) {
        Ball ball = balls.get(i);
        ball.stopPos+=150;
      } // for
      yPos = mouseVar * 150;
      mouseVar = (mouseVar+1) % 5;
      mouseVar++;
      rCol = random(255);
      gCol = random(255);
      bCol = random(255);
      balls.add(new Ball(xPos*mouseVar, yPos, 
        rectWidth, rectHeight, 
        beginPosition, stopPosition, 
        rCol, gCol, bCol));
    } // func 
    
    // ==================================================
    
    class Ball {  
      // pos
      float x;
      float y;
      // size
      float w;
      float h;
      // movement 
      float beginPos;
      float stopPos;
      float speed = 5;
      // color // or use color 
      float r, g, b;
    
      Ball(float tempX, float tempY, 
        float tempW, float tempH, 
        float tempBegin, float tempStop, 
        float tempR, float tempG, float tempB) {
        x = tempX;
        y = tempY;
        w = tempW;
        h = tempH;
        beginPos = tempBegin;
        stopPos = tempStop;
        r = tempR;
        g = tempG;
        b = tempB;
      }
    
      void moveThis() {
        y = y + speed;
        if (y > stopPos) {
          y = stopPos;
        }
      }
    
      void display() {
        fill(r, g, b);
        rect(x, y, w, h);
      }
      //
    } // class
    //
    
  • edited January 2017

    .

This discussion has been closed.