当我的程序来到这个方法时,它似乎永远不会更新目标值。如果输入“dave”,则无论调用多少次该方法,它都将保持“dave”。我的链表没有返回正确的值或正确更新列表
public Person lookup(String name){
if(firstPerson == null){
return null;
}
Person target = null;
for (target = firstPerson; target != null; target = target.nextPerson){
if(name.equals(target.name)){
return target;
}
else {
return null;
}
}
return target; // replace this line
}
如果我通过这个addFriend方法添加好友,firstFriend将最终打印最后添加的名字。如果输入的名字是rob bill和travis 输出将是travis travis travis。
public void addFriend(Person friend){
firstFriend = new Friend(friend, firstFriend);
return; // replace this line
public String friendString(){
String friendList = "";
if(firstFriend == null){
return null;
}
for(Friend pointer = firstFriend; pointer != null; pointer = pointer.nextFriend){
friendList = friendList + firstFriend.who.name + " ";
}
return friendList.trim(); // replace this line
}
你的查找方法中提到的其他部分不正确。不应该有别的声明。如果它通过整个for循环而没有找到“target”,它应该返回null。 – Compass
谢谢@Compass。那么如果我遍历列表而没有找到目标,我的最终返回值是否为空? –
是的。在合同模型中,您的合同是*返回目标,如果找到*,如果找不到,则返回null。您提供的模型改为遵守合同*返回目标,如果它是第一个在列表中,否则返回null *。 – Compass