java 计数,在ArrayList中计数-Java

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[i]=new Hand();

}

for(int i=0; i<=Deck.size()+8; i++) {

for(Hand hand:hands) {

hand.addSingleCard(Deck.deal());

}

}

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值