Chris Donald

Greenhorn

Posts: 23

posted 8 years ago

Hey Folks,

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

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

posted 8 years ago

But your example does no looping. You just have a set number of instance hard-coded 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...

Chris Donald

Greenhorn

Posts: 23

posted 8 years ago

Thanks Bear for your reply.What I meant to say was the following:

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 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

Chris Donald

Greenhorn

Posts: 23

Sam Aran

Greenhorn

Posts: 12

posted 8 years ago

I don't know what "rank" is, so I'm going to use a general collection for this, but you'll need to use a specific one for "rank" if you want to get rid of the inevitable warning.

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.

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.

Chris Donald

Greenhorn

Posts: 23

posted 8 years ago

Thanks for your reply.The HashSet is useful.I have another question I don't have an answer to.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?

Please advise.

Thanks Chris

Please advise.

Thanks Chris

Guido Sautter

Ranch Hand

Posts: 142

posted 8 years ago

Hi 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 un-intuitive, but it's the (zero-based) way Java is, just as with Calendar.JANUARY = 0 ... Now let's group the cards:

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 un-intuitive, but it's the (zero-based) way Java is, just as with Calendar.JANUARY = 0 ... Now let's group the cards:

posted 8 years ago

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.

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 un-intuitive, but it's the (zero-based) 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

Nina Milo

Greenhorn

Posts: 19

posted 8 years ago

****************

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 ]

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 ]

Campbell Ritchie

Sheriff

Posts: 53750

127

posted 8 years ago

Nina Milo, please find the code button; I have edited your post to show how much better it is to read.

What does that method do? It looks very convoluted.

What does that method do? It looks very convoluted.

Chris Donald

Greenhorn

Posts: 23

Guido Sautter

Ranch Hand

Posts: 142

posted 8 years ago

Hi Rob,

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 ...

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 ...