This question already has an answer here:
I have an ArrayList filled with objects of the class Result. Every Result has 3 attributes named number, category and value. I have made a for-loop with if-statements, which is supposed to compare results from instances of Result. If two Result shares number & category. The one with the lesser in value should be removed from the ArrayList.
Right now it works apart from sometimes, when the second highest value doesnt seem to be recorded. Can one of you find out what Ive done wrong?
ArrayList<Result> copylist = new ArrayList<Result>();
Result matchedResult = null;
for (int i = 0; i < resultlist.size(); i++) {
matchedResult = resultlist.get(i);
copylist.add(matchedResult);
}
Collections.sort(copylist);
for (int i = 0; i < copylist.size(); i++) {
Result rezul = copylist.get(i);
for (int z = 1; z < copylist.size(); z++) {
Result rezul2 = copylist.get(z);
if (rezul.number() == rezul2.number()
&& rezul.category().equals(rezul2.category())) {
if (rezul.value() >= rezul2.value()) {
copylist.remove(rezul2);
} else if (rezul2.value() >= rezul.value()) {
copylist.remove(rezul);
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire