Problems setting up an arrayList

edited April 2017 in Questions about Code

I did this before and I checked the processing reference but I constantly get an error message regarding my array list. The function get(int) doesnt exist. Im probably overseeing sth super simple, but i cant figure it out right now. Would be great if you can have a look Here is a simplified version of the code Thanks a lot!!

Tile tile;

void setup() {
  tile=new Tile(0,0,width,height);
}

void draw() {
   tile.display();
}


    class Tile {
      float x;
      float y;
      float w;
      float h;

      ArrayList<Food>grapes;

      Tile(float posX, float posY, float Width, float Height) {
        x=posX;
        y=posY;
        w=Width;
        h=Height;

        grapes=new ArrayList<Food>();
        grapes.add(new Food(200,250));
        grapes.add(new Food(100,200));

      }

      void display() {
        pushStyle();
        fill(200, 200, 0);
        rect(x, y, width, height);
        popStyle();

        for (int i=0; i<grapes.size(); i++) {
          Food grapes = grapes.get(i);
          grapes.displayGrape();
        }
      }
    }



     class Food {
      float x;
      float y;


      PImage imgGrape, imgApple;

      Food (float foodX, float foodY) {
        x=foodX;
        y=foodY;

        imgGrape=loadImage("grape.png");
        imgApple=loadImage("apple.png");
      }

      void displayGrape() {
        image(imgGrape, x, y);
      }

      /*void displayApple() {
        image(imgApple, x, y);
      }*/
    }

Answers

  • Answer ✓

    Look at this line:

    Food grapes = grapes.get(i);

    You declare a local variable of type "Food", it has the same name as your ArrayList. Try to use a different name here, then it should work.

  • Line 18 and 39 have something in common. But they are two different things.

    Kf

Sign In or Register to comment.