Reputation: 697
First-timer here, having a problem with this here Java code: import java.util.*;
public class Pootis
{
public static void main(String[] args)
{
Superhero batman = new Superhero("Bruce", 26, "Batman");
Human rachel = new Human("Rachel", 24);
Superhero ironman = new Superhero("Tony", 35, "Ironman");
Human pepper = new Human("Pepper", 22);
List<Human> people = new ArrayList<>();
people.add(batman);
people.add(rachel);
people.add(ironman);
people.add(pepper);
Collections.sort(people);//<-----
}
}
The purpose of this program is to sort the people in the people ArrayList by age. I am using the comparable interface to this. The problem appears to be when the Collections.sort(people) is called. What did I do wrong?? This is the second class:
public class Human implements Comparable<Human> {
private int age;
private String name;
public Human(String givenName, int age) {
this.name = givenName;
this.age = age;
}
@Override
public int compareTo(Human other){
if(getAge() > other.getAge()){
return 1;
}
else if(getAge() < other.getAge()){
return -1;
}
return 0;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
This is the error:
Pootis.java:65: error: no suitable method found for sort(List) Collections.sort(people); ^ method Collections.sort(List,Comparator) is not applicable (cannot instantiate from arguments because actual and formal argument lists differ in length) method Collections.sort(List) is not applicable (inferred type does not conform to declared bound(s) inferred: Object bound(s): Comparable) where T#1,T#2 are type-variables: T#1 extends Object declared in method sort(List,Comparator) T#2 extends Comparable declared in method sort(List) 1 error
This is the Superhero class:
public class Superhero {
String alterEgo, name;
int age;
public Superhero(String givenName, int age, String alterEgo) {
super();
this.alterEgo = alterEgo;
this.name = givenName;
this.age = age;
}
public String getAlterEgo() {
return alterEgo;
}
public String introduce() {
return "Hey! I'm " + name + " and I'm " + age + " years old. I'm also known as" + alterEgo + "!";
}
}
Upvotes: 0
Views: 2313
Reputation: 4100
The signature of Collections.sort()
is:
public static <T extends Comparable<? super T>> void sort(List<T> list)
This means that you can only call sort
on a List of Comparable
objects. Using List<Object>
is not valid as Object
is not a Comparable
. sort()
is overloaded with
public static <T> void sort(List<T> list,
Comparator<? super T> c)
but this is not applicable to your method call as you've only provided one parameter.
Bear in mind that, as per the javadoc, in order to sort a list using Collections.sort()
all the elements must be mutually comparable. In your case it means that you should have a way of comparing a Human
to a Superhero
. Also, if Superhero extends Human
, your list should be declared as a List<Human>
(or viceversa).
Upvotes: 1
Reputation: 22005
If Superhero
extends Human
, your list should look like
List<Human> persons = new ArrayList<>();
Because your objects inside the list are of type Object
if you do it in another way and thus they can not be compared since compareTo
is overridden in Human
Upvotes: 0