将矩形添加到ArrayList

时间:2017-12-24 00:48:43

标签: arraylist libgdx

我正在尝试使用addAll将多个矩形添加到ArrayList(“rectPlatform”)中。 我要添加到ArrayList的矩形是:

Rectangle rectOne;
Rectangle rectTwo;
Rectangle rectThree;

我尝试了很多,但是我没有成功地尝试addAll矩形进入rectPlatform。 请有人帮帮我吗?

1 个答案:

答案 0 :(得分:1)

AddAll仅适用于集合,通常是ListQueueSet接口实现类。因此,为了使您的代码有效,您的Rectangle对象必须已经在集合中。

您也可以尝试:

List<Rectangle> rectList = new ArrayList<>(Arrays.asList(new Rectangle[]{rectOne, rectTwo, rectThree}));

或另一种方式:

List<Rectangle> list1 = new ArrayList<>();
list1.add(rectOne);
list1.add(rectTwo);
list1.add(rectThree);
List<Rectangle> list2 = new ArrayList<>(list1);
相关问题