我正在将篮球传递给联系袋移除方法。我检查了我已经传入的同一个对象。它返回false。
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;
}
这是篮球课
公共班级篮球实施可比性
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());
}
答案 0 :(得分:0)
您是否尝试在BasketBall课程中制作成本'equals' ovveride ?如果不是java默认使用“对象”JavaDoc Object中的那个,这可能会或可能不会起作用,具体取决于类的制作方式。例如,尝试stackoverflow post
在java中,true被转换为值1(和false 0),因此它永远不会相等,无论是在这里返回0还是使用等于覆盖,如果不需要知道排序的等级
答案 1 :(得分:0)
我认为您最好尝试覆盖equals(),而不是定义compareTo(),因为您在remove()方法中使用了point.getData()。equals()。
或者您可以在remove()方法中使用自己的compareTo(),而不是equlas()。