public void notMyFriend(Student student1) {
System.out.println("Friends who are not my friends:");
int i;
for (i = 0; i < student1.friendList.size();i++) {
for (int j = 0; j < friendList.size(); j++) {
if (student1.friendList.get(i) == friendList.get(j)) continue;
else {
System.out.println(student1.friendList.get(i).getName());
}
}
}
}
那个代码片段确实打印了我的朋友和我朋友的朋友,这些朋友和朋友不是互相帮助的。我不明白为什么?有人可以帮助我吗?
学生:
public class Student {
private int Number;
private String Name;
private String Gender;
private List<Student> friendList = new ArrayList<Student>();
}
答案 0 :(得分:3)
如果您的学生班级具有equals()
的合适实施,您可以使用removeAll()
hisFriends.removeAll(yourFriends);
System.out.println(hisFriends); // not your friends
答案 1 :(得分:0)
在不使用设定操作的情况下打印不相互的朋友:
public void notMyFriend(Student student1) {
System.out.println("Friends who are not my friends:");
int i;
for (i = 0; i < student1.friendList.size();i++) {
for (int j = 0; j < friendList.size(); j++)
if (student1.friendList.get(i) == friendList.get(j)) break;
if(j==friendList.size())
System.out.println(student1.friendList.get(i).getName());
}
}