java - 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>();
}
解决方案
如果你的 Student 类有一个合适的实现equals()
,你可以使用removeAll()
hisFriends.removeAll(yourFriends);
System.out.println(hisFriends); // not your friends
推荐阅读
- amazon-web-services - 私有 VPC 中的 Lambda 需要很长时间才能使用 S3 API
- java - 解锁手机后重新启动应用程序覆盖
- for-loop - 如何使用循环插入面板数据中的值
- java - 我可以在服务 bean 中有公共静态字段吗?
- c++ - 在主函数的参数括号中使用 void 进行函数重载
- python - 为什么我的 Streamlit 应用程序会多次打开?
- flutter-test - 如何为 Flutter 方法编写单元测试,该方法稍后在未来完成?
- batch-file - 批量添加文件夹到多个 rar 文件
- sql-server - 将 PostgreSQL 查询转换为 SQL Server 查询
- json - 如何以角度将json动态写入本地json