I am currently writing class that creates multiple hands containing card objects using arrayLists. I am trying to write a method that searches an arraylist (hand) and returns the largest pair of cards.
Here is what I have now but it is very inefficient:
public int findPairRank() {
int one = 0, two = 0, three = 0, four = 0, five = 0, six = 0, seven = 0, eight = 0, nine = 0, ten = 0, eleven = 0, twelve = 0, thirteen = 0;
// loops through every card rank and adds +1 if it exists in the hand
for (int i = 0; i < cards.size(); i++) {
if (cards.get(i).getRank() == 1)
one++;
if (cards.get(i).getRank() == 2)
two++;
if (cards.get(i).getRank() == 3)
three++;
if (cards.get(i).getRank() == 4)
four++;
if (cards.get(i).getRank() == 5)
five++;
if (cards.get(i).getRank() == 6)
six++;
if (cards.get(i).getRank() == 7)
seven++;
if (cards.get(i).getRank() == 8)
eight++;
if (cards.get(i).getRank() == 9)
nine++;
if (cards.get(i).getRank() == 10)
ten++;
if (cards.get(i).getRank() == 11)
eleven++;
if (cards.get(i).getRank() == 12)
twelve++;
if (cards.get(i).getRank() == 13)
thirteen++;
}
ArrayList<Integer> list = new ArrayList<Integer>();
if (one == 2)
list.add(1);
if (two == 2)
list.add(2);
if (three == 2)
list.add(3);
if (four == 2)
list.add(4);
if (five == 2)
list.add(5);
if (six == 2)
list.add(6);
if (seven == 2)
list.add(7);
if (eight == 2)
list.add(8);
if (nine == 2)
list.add(9);
if (ten == 2)
list.add(10);
if (eleven == 2)
list.add(11);
if (twelve == 2)
list.add(12);
if (thirteen == 2)
list.add(13);
int max = 0;
for (int i = 0; i < list.size(); i++) {
if (list.get(i) > max)
max = list.get(i);
}
if (max > 0)
return max;
else
return 0;
}