1

I'm getting a compiler error at line 80:

Error: /Users.../AsciiDisplay.java:80: cannot find symbol

Symbol: method draw(AsciiDisplay)

Location: class java.lang.Object

public class AsciiDisplay {

  private char [][] grid;
  private ArrayList shapes;

  public AsciiDisplay() {
    grid = new char [30][15];
    shapes = new ArrayList();
  }

  public void updateGrid() {

    for(int i = 0; i < shapes.size(); i++) {
      shapes.get(i).draw(this); //Line 80: The error is in this line of code.
    }
  }
}

public class Shape {

  protected String id;
  protected Coordinate location;

  public Shape(String id, Coordinate location) {
    this.id = id;
    this.location = location;
  }

  public void draw(AsciiDisplay dis) {
    dis.putCharAt(location.getX(),location.getY(),'?');
  }
}
4

1 回答 1

0

What you want is ArrayList of shapes so you need to declare your ArrayList that way.

ArrayList<Shape> shapes;

Now compiler will know that it only contains Shapes so it will allow you to call methods on shapes on elements of list.

You can have a look at Generics Tutorial for more info.

于 2012-10-23T17:52:11.047 回答