2012-11-15 15 views
0

更新的代碼計數在一個ArrayList - Java的

我試圖找出一種方法來計算一個值出現的頻率的ArrayList。

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

所以在上面的情況下,我想統計有多少「俱樂部」在手[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; 
} 

我畫一個空白....如果我改變方法類型ArrayList的話,我不能返回 「計數」

手[0]創建:

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()); 
      } 
     } 
+1

if(suit == suit)?這裏缺少的東西。 – kosa

+0

'hands [0]'是什麼? –

+1

這將始終返回true:'if(suit == suit)' –

回答

2

您可以從Collections類獲得此功能。它是方法

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

這將返回集合內特定對象的發生次數。請注意,如果沒有提供方法equals,那麼這將不起作用,或者至少不會與您想到的平等一致,因爲Java不知道您的平等意義。

+0

我試圖使用你的方法,但它只返回1計數...也許我忽略了一些簡單的.. 。 – binary101

相關問題