1

I have created a default constructor which creates an empty "hand".

  public Hand() {
    hand = new ArrayList();
  }

Whats the most efficient way to have a second constructor take an Array of cards, then add them a hand?

2
  • 2
    Most efficient will be to store the same reference if the given array - but there are a LOT of draw backs in it. Commented Nov 14, 2012 at 18:51
  • In all games I know a hand does not contain many cards. Worrying about the most efficient way of doing this sound like premature optimization. I'd prefer the most clear/flexible way. Commented Nov 14, 2012 at 19:00

3 Answers 3

7

I would have one constructor to do both.

public Hand(Card... cards) {
    hand = Arrays.asList(cards);
}

Or an ArrayList copy as Rohit Jain suggests.

Sign up to request clarification or add additional context in comments.

6 Comments

Note: Arrays.asList gives a fixed size ArrayList. of type: - java.util.Arrays.ArrayList
But then he cannot add any cards to hand later, and the fact that he has a no-args constructor suggests that he would want to, doesn't it?
@RohitJain: It should be fine - there is no reason why hand won't be a List.
@amit.. Actaully, problem is not with the type of hand. Problem is that, it is unmodifiable list. That's why I used a constructor in my version.
It's hard to know what the requirements are but this is the simplest. You might copy-on-write the hand or want to make it mutable with a copy.
|
4

You can do it like this: -

public Hand(String[] hands) {
    hand = new ArrayList<String>(Arrays.asList(hands));
}

Or, you can iterate over your string array, and add individual elements to your ArrayList.

public Hand(String[] hands) {
    hand = new ArrayList<String>();

    for (String elem: hands) 
         hand.add(elem);
}

P.S: - Always declare a Generic Type List.

8 Comments

This is definetly not most efficient
@amit.. Do you suggest iterating over the array and adding it to the ArrayList?
No, I suggest a plain and stupid reference store or Arrays.asList alone (which fits perfectly assuming hand is a List, as it should be)
@amit.. Well, you know that Arrays.asList returns a fixed size list right? So, if you just assign it, you won't be able to modify it.
@amit: If you think this is a good solution, why do you advice against it in your comment below the question?
|
0

There's another option, Collections.addAll:

public Hand(Card[] cards) {
    hand = new ArrayList<Card>();
    Collections.addAll(hand, cards);
}

According to the documentation:

Adds all of the specified elements to the specified collection. Elements to be added may be specified individually or as an array. The behavior of this convenience method is identical to that of c.addAll(Arrays.asList(elements)), but this method is likely to run significantly faster under most implementations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.