Casting between ArrayLists in Java

后端 未结 6 635
故里飘歌
故里飘歌 2020-12-15 11:56

Sorry, I thought this was an inheritance question: it was an ArrayList question all along!

Ok, my problem is more specific than I thought. So I have two families of

6条回答
  •  眼角桃花
    2020-12-15 12:21

    One way to do that is by casting to ArrayList first:

    ArrayList cards = (ArrayList)(ArrayList) (pokerCardObjects);
    

    Another alternatives without casting:

    With streams:

    ArrayList cards = pokerCardObjects.stream().collect(Collectors.toCollection(ArrayList::new);
    

    Or creating a new ArrayList:

    ArrayList cards = new ArrayList<>(pokerCardObjects);
    

提交回复
热议问题