J'ai suivi un tutoriel, mais je n'ai pas réussi à faire ma Country
classe Comparable
pour mon BST
.Rendre votre propre classe 'comparable'
principal:
BinarySearchTree A = new BinarySearchTree();
Country a = new Country("Romania", "Bucharest", 1112);
A.insert(a);
classe Pays:
public int compareTo(Object anotherCountry) throws ClassCastException {
if (!(anotherCountry instanceof Country))
throw new ClassCastException("A Country object expected.");
String anotherCountryName = ((Country) anotherCountry).getName();
int i = this.name.compareTo(anotherCountryName);
if(i < 0){
return -1;
} else {
return 0;
}
}
erreur:
@Override
public int compareTo(Object anotherCountry) throws ClassCastException {
if (!(anotherCountry instanceof Country))
throw new ClassCastException("A Country object expected.");
String anotherCountryName = ((Country) anotherCountry).getName();
return this.name.compareTo(anotherCountryName);
Description Resource Path Location Type
choc Nom: La méthode compareTo (Object) du pays de type a le même effacement que compareTo (T) de type Comparable mais ne le remplace pas Pays.java/Lab2_prob 4/src ligne 17 Java Problème
Description Resource Path Location Type
The method compareTo(Object) of type Country must override or implement a supertype method Country.java /Lab2_prob 4/src line 17 Java Problem
et classe:
public class Country implements Comparable<Country>{
private String name;
private String capital;
private int area;
Description Resource Path Location Type
Le Pays de type doit mettre en œuvre la méthode abstraite héritée Comparable.compareTo (Pays) Country.java/Lab2_prob 4/ligne src 2 Java Problème
Votre classe 'Country' étend-elle' Comparable '? –
Quel problème ou erreur obtenez-vous réellement? – DNA
désolé vient d'ajouter l'erreur. –