Remember, if you want to iterate over all the items in a collection, you can use the for-each
form:
for (YourClass item : collection) {
// do something with item
}
http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html
EDIT: just to show a way to use the iterator.
int nextToCompare = 1; // the index of the next item in the array to compare with the current item
for (Item item : this.hand) {
if (nextToCompare < this.hand.length // checks if there is next item to compare
&& item.getRank() == this.hand[nextToCompare++].getRank()) {
return true;
}
}
return false;
One cons of this method is that it iterates trough the entire array, rather than over n - 1
elements.
I think the method you posted is actually a good solution, in terms of efficiency and clarity.