So I have seen multiple questions addressing similar problems to mine, but I was not able to find one that was exactly like my problem.
I have an ArrayList of Contact objects, and I want to sort them using Collections.sort:
public void sortContactArrayList(){
ArrayList<Contact> newList = new ArrayList<Contact>();
Collections.sort(newList);
}
In order to do this, I made Contactimplement Comparable<Contact>.
And, my compareTo method looks like this:
@Override
public int compareTo(Contact otherContact) {
return this.getName().compareTo(otherContact.getName());
}
However, I am receiving an error when I am calling Collections.sort(newList);
The error is:
"Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (ArrayList<Contact>). The inferred type Contact is not a valid substitute for the bounded parameter <T extends Comparable<? super T>>"
Does anyone know what the issue is? Like I said, I have seen this similar problem with a customized list of certain objects like "ContactDatabase<Contact>" or something, but I have never seen this problem with just a certain object itself.
Thank you!
Contactdoes not implementComparable<Contact>. Are you sure you've recompiled it and your compiler is looking at the right .class file?Contact?