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>();
    }

最佳答案

如果您的学生班级具有equals()的合适实现,则可以使用removeAll()

hisFriends.removeAll(yourFriends);
System.out.println(hisFriends); // not your friends

10-06 02:32