Convert an array into an ArrayList

Saatana picture Saatana · Mar 21, 2012 · Viewed 126k times · Source

I'm having a lot of trouble turning an array into an ArrayList in Java. This is my array right now:

Card[] hand = new Card[2];

"hand" holds an array of "Cards". How this would look like as an ArrayList?

Answer

Kal picture Kal · Mar 21, 2012

This will give you a list.

List<Card> cardsList = Arrays.asList(hand);

If you want an arraylist, you can do

ArrayList<Card> cardsList = new ArrayList<Card>(Arrays.asList(hand));