Search code examples
arraylistlibgdx

Adding rectangles to an ArrayList


I'm trying to add more than one rectangle into an ArrayList ("rectPlatform") using the addAll for it. The rectangles I want to add to my ArrayList are:

Rectangle rectOne;
Rectangle rectTwo;
Rectangle rectThree;

I tried a lot, but I didn't get any success trying to addAll rectangles into the rectPlatform. Can anybody help me please to do that?


Solution

  • AddAll only works for collections, normally the List, Queue and Set interface implementing classes. So to make your code work, your Rectangle objects would have to already be in a collection.

    You could also try:

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

    Or another way:

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