1

UPDATED CODE

I'm trying to figure out a way to count how often one value appears in an ArrayList.

System.out.println("Hand 1, Number of Clubs: " 
              + hands[0].frequency(hands[0], Card.Suit.CLUBS));

So in the above case i want to count how many "Clubs" are in hands[0].

public int frequency(Hand c, Suit suit){
    int count = 0;
    if(suit == suit) //need this to be "If suit (club) is present, count++)
            {
             count++;
            }    
    return count;
}

I'm drawing a blank.... If i change the method type to ArrayList then i cannot return "count"

hands[0] is created by:

    Hand[] hands = new Hand[4];
    for(int i=0; i<hands.length; i++) {
       hands[i]=new Hand();
       }
       for(int i=0; i<=Deck.size()+8; i++) {
          for(Hand hand:hands) {
             hand.addSingleCard(Deck.deal());
           }
       }
6
  • 1
    if(suit == suit) ? something missing here. Commented Nov 15, 2012 at 14:54
  • 1
    This will always return true: if(suit == suit) Commented Nov 15, 2012 at 14:55
  • 4
    I suggest you start with something which compiles. Commented Nov 15, 2012 at 14:55
  • For better help sooner, post an SSCCE. Commented Nov 15, 2012 at 14:57
  • It does compile. Although i have been playing around with it. hands[0] is an arrayList of cards (approximates 13 cards). I want to pass the method the hands[0] arrayList and possibly a suit and it return an int of how many of that suit is present in the arrayList. Commented Nov 15, 2012 at 15:05

1 Answer 1

3

Collections.frequency

You have this feature already available from Collections class, in the frequency method.

public static int frequency(Collection<?> c, Object o)

That will return the number of occurrences of a specific object inside a collection.

Mind that without providing a vaild equals method this won't work or at least won't work with the equality you have in mind, since Java does't know your meaning of equality.

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

1 Comment

I've attempted to use your method, however it only returns 1 count... maybe i'm overlooking something simple...

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.