I have a collection of 5 Java Beans(eg:Card Bean).One of the properties of the Bean is rank.What is the best way to check if there are 3 bean instances that have the same rank?I know that one way of doing this is to loop through the collection
for eg:
if((cardA.getRank()==cardB.getRank())&&(cardB.getRank()==cardC.getRank())&&(cardC.getRank()==cardD.getRank())&&(cardD.getRank()==cardE.getRank())){
return true;
}
The above is only one combination.There will be others like comparing cardA to cardC or cardD or cardE.Is there a more elegant way of checking if there are atleast 3 cards have the same rank?
Thanks
Chris
But your example does no looping. You just have a set number of instance hardcoded into an if. How could you write a true loop that would be independent of the number of items in the collection?Originally posted by Chris Donald:
I know that one way of doing this is to loop through the collection...
I have a method like shown below:
public boolean isSameRank(Card cardA,Card cardB,Card cardC,Card cardD,Card cardE){
if((cardA.getRank()==cardB.getRank())&&(cardB.getRank()==cardC.getRank())&&(cardC.getRank()==cardD.getRank())&&(cardD.getRank()==cardE.getRank())){
return true;
}
return false;
}
The goal here is to find out if there are atleast 3 cards here with the same rank.Instead of adding more AND OR Clauses to the code shown above,is there a better way of doing things.
Please advise.
Thanks
Chris
I'm assuming your cards *are* in a collection. Since I'm using general collections, I'll use a standard for loop, if you use generics, you can convert it to enhanced.
If three or more cards have the same rank, a rank entry will be inserted for the first one, but not the two duplicates, so you compare it to the total number of possible choices (independent ranks) minus the two duplicates. You needed "at least", so if it is less than that (for instance, there are 4 cards with the same rank), the set will have less and this will be true.
Please advise.
Thanks Chris
a more general way of doing this would be to group the cards by rank. I assume that you have constants for the ranks, like Card.ACE = 0, Card.TWO = 1, Card.THREE = 2, ..., Card.KING = 12. I know the numbers are a little unintuitive, but it's the (zerobased) way Java is, just as with Calendar.JANUARY = 0 ... Now let's group the cards:
Originally posted by Guido Sautter:
[QB]a more general way of doing this would be to group the cards by rank. I assume that you have constants for the ranks, like Card.ACE = 0, Card.TWO = 1, Card.THREE = 2, ..., Card.KING = 12. I know the numbers are a little unintuitive, but it's the (zerobased) way Java is, just as with Calendar.JANUARY = 0 ...
And Calendar.SUNDAY = 1
It doesn't really matter what value you give them; you could start at 481 if you like. That's what you are naming them for.
I'd start at 1 in this case.
SCJP 1.4  SCJP 6  SCWCD 5  OCEEJBD 6  OCEJPAD 6
How To Ask Questions How To Answer Questions
Lets say that I have a Collection of 10 numbers.
Is there a nice way to find out the number of occurences of
a particulat number in the collection?For ex: number of times
the numbers 5,4 and 7 occur in the collection.I know that we
could use a counter to do so.But I am wondering if there is a
better way of doing so?
*****************
Chris,
I have a simple solution to find the occurence of a single number within the collection as you requested without using the counters. You can create a simple collection of 10 numbers as you wish and try using this code snippet to find the occurenece of single number.
Thanks.
***************************************************************
********************************************
[ September 08, 2008: Message edited by: Nina Milo ]
[ September 08, 2008: Message edited by: Nina Milo ]
[edit]Add code tags. CR[/edit]
[ September 08, 2008: Message edited by: Campbell Ritchie ]
What does that method do? It looks very convoluted.
Originally posted by Rob Prime:
And Calendar.SUNDAY = 1
It doesn't really matter what value you give them; you could start at 481 if you like. That's what you are naming them for.
I'd start at 1 in this case.
surely vou can sart at 481, but then you would have to do adjustment computations in order to use the constants as array indices ... that's why I proposed starting with Card.ACE = 0 ... Otherwise, you'd hav to subtract Card.ACE all the time, and still asume that the card rank constants are subsequent, distinct integers of which Card.ACE is the smalest ...
Acetylsalicylic acid is aspirin. This could be handy too:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
