如何更改存储在数组中的对象的值

时间:2018-12-03 03:15:52

标签: java oop bluej

我正在尝试使用set()方法更改存储在数组列表中的对象的值。但是,当我调用方法并输入参数时,遇到以下错误:

  

找不到合适的方法来设置(参数)

    //Objects are shapes composed of a color, fill and dimensions
    ArrayList<Shape> shapes = new ArrayList<>();

    //add triangle to shapes
    shapes.add(new Triangle("white", true, 3.0, 2.5, 2.0));
    //add rectangle to shapes
    shapes.add(new Rectangle("red", true, 2.0, 4.0));
    //add circle to shapes
    shapes.add(new Circle("yellow", false, 1.0));

    System.out.println("Starting Shapes");

    //traverse shapes
    for (Shape s : shapes)
        System.out.println(s.toString());

    System.out.println("Changed Shapes");
    shapes.set(2, false);

如何正确更改数组列表中存储的对象的参数?例如,将三角形从true更改为false,将矩形从红色更改为黄色。

0 个答案:

没有答案