首页 > 解决方案 > My task is to print the friends of my friends, that are not my friends

问题描述

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

        }
    }

That code fragment does print my friends and the friends of my friends that are mutual for us,not the friends that are not mutual.I cant understand why? Do someone can help me?

Student:

public class Student { 
    private int Number; 
    private String Name; 
    private String Gender; 
    private List<Student> friendList = new ArrayList<Student>();
    }

标签: java

解决方案


如果你的 Student 类有一个合适的实现equals(),你可以使用removeAll()

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

推荐阅读