Ich bin im Basketball in die verknüpfte Tasche Remove-Methode übergeben. Ich überprüfe das gleiche Objekt, das ich bereits übergeben habe. Es wird falsch zurückgegeben.Ich kann nicht herausfinden, warum die Methode falsch zurückgibt
public static void removeMain(Scanner input,LinkedBag<Basketball> teamX){
System.out.println("Please enter the team & rank of the object you want to remove.");
System.out.println("Team?");
String team = input.nextLine();
System.out.println("Rank?");
int rank = input.nextInt();
input.nextLine();
if(teamX.remove(new Basketball(team, rank))){
System.out.println("REMOVED!");
}else{
System.out.println("Team was not found due to it not being in the list.");
}
}
public boolean remove(E target){
boolean found = false;
int i = 0;
Node<E> pointer = head;
Node<E> previous = head;
while (pointer != null && !found){
if((pointer.getData()).equals(target)){
found = true;
}else{
pointer = pointer.getLink();
if(i>0){
previous = previous.getLink();
}
i++;
}
}
if(found){
previous.setLink(pointer.getLink());
numElements--;
}
return found;
}
Dies ist die Basketball-Klasse
public class Basketball implementiert Vergleichbare
public int compareTo(Basketball anotherTeam)
throws ClassCastException
{
if (!(anotherTeam instanceof Basketball))
throw new ClassCastException("A Car object expected.");
if (getRanking() < anotherTeam.getRanking())
return -1;
else if (getRanking() > anotherTeam.getRanking())
return 1;
else
return team.compareToIgnoreCase(anotherTeam.getTeam());
}
Bitte schreiben Sie den Code als Text, und nicht als Bild –
Wie mache ich das –
kopieren Sie den Text, und fügen Sie es in der Post. –