在ArrayList中计数-Java [英] Counting in an ArrayList - Java
问题描述
更新代码
UPDATED CODE
我试图找出一种方法来计算一个值在ArrayList中出现的频率.
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));
因此,在上述情况下,我想计算手中有多少俱乐部" [0].
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;
}
我正在绘制一个空白....如果我将方法类型更改为ArrayList,那么我将无法返回"count"
I'm drawing a blank.... If i change the method type to ArrayList then i cannot return "count"
手[0]由以下人员创建:
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());
}
}
推荐答案
Collections.frequency
您已经从 Collections
类,位于
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.
这篇关于在ArrayList中计数-Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!