Convert an array into an ArrayList [duplicate] Convert an array into an ArrayList [duplicate] arrays arrays

Convert an array into an ArrayList [duplicate]


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));


As an ArrayList that line would be

import java.util.ArrayList;...ArrayList<Card> hand = new ArrayList<Card>();

To use the ArrayList you have do

hand.get(i); //gets the element at position i hand.add(obj); //adds the obj to the end of the listhand.remove(i); //removes the element at position ihand.add(i, obj); //adds the obj at the specified indexhand.set(i, obj); //overwrites the object at i with the new obj

Also read this http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html


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