How do you create a constructor that takes and array of cards and adds them to a hand which is an arrayList? Below are my 3 constructors so far…
“The class should provide a default constructor(creates an empty hand), a constructor that takes an array of cards and adds them to the hand and a constructor that takes a different hand and add the cards to this hand”
Updated:
public class Hand {
private List<Hand> hand = new ArrayList<Hand>();
public Hand(){
hand = new ArrayList<Hand>();
}
public Hand(Card[] cards){
//this.hand.addAll(Arrays.asList(cards));
//this.hand = new ArrayList<Hand>(Arrays.asList(cards));]
}
public Hand(Hand cards){
this.hand = Arrays.asList(cards);
}
}
You should have the list as the instance variable in the
Handclass:Currently you are declaring a local variable which goes out-of-scope immediately after the constructor returns.
Update
It doesn’t make sense to have a list of
Hands within theHandclass itself. IMO, it would be better to let eachPlayerkeep their ownHand.As far as understand, you want to have a constructor that initializes the list of cards, and a method to add cards to that list. Something as follows:
Following are the constructors: