gpt4 book ai didi

java - 在集合中添加元素

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:50:42 24 4
gpt4 key购买 nike

我必须创建一个有一些本科生和研究生的类(class),然后使用方法 getPostgraduates() 从类(class)中提取所有以“Ismael Bento”为导师的研究生,并使用类 Notifier 向他们发送消息(打印文本和收件人)。但是,没有打印任何内容...我的猜测是我的 getPostgraduates() 方法有问题。

这里是主要方法:

package students;

import java.util.*;

public class ProgrammingTest {

public static void main (String[] args){
Academic rr = new Academic("Ricardo Rodriguez");
Academic ib = new Academic("Ismael Bento");
Set<Student> students = new HashSet<Student>();

Undergraduate ug1 = new Undergraduate("gg4", "Greg","gg4@", rr);
Undergraduate ug2 = new Undergraduate("pr3", "Pete","pr3@", ib);
Postgraduate pg1 = new Postgraduate("te2", "Ted", "te2@", rr);
Postgraduate pg2 = new Postgraduate("yj34", "Yao", "yj34@", ib);
Postgraduate pg3 = new Postgraduate("jj8", "Jack", "jj8@", ib);

students.add(ug1);
students.add(ug2);
students.add(pg1);
students.add(pg2);
students.add(pg3);

Course c1 = new Course("c1", students);
Set<? extends Notifiable> n = c1.getPostgraduates("Ismael Bento");
Notifier notifier = new Notifier(n);
notifier.doNotifyAll("You have been notified!");

}

}

和类(class)类(class):

package students;

import java.util.*;

public class Course {

private Set<Student> students;
private String name;

public Course (String name, Set<Student> students){
this.name = name;
this.students = students;
}

public Set<Postgraduate> getPostgraduates(String nameOfSupervisor){
Set<Postgraduate> postgraduates = new HashSet<Postgraduate>();

for(Postgraduate p : postgraduates) {
if (p.getSupervisor().equals(nameOfSupervisor)){
postgraduates.add(p);
}
}
return postgraduates;

}

}

和通知类:

package students;
import java.util.Iterator;
import java.util.Set;

public class Notifier {
Set<? extends Notifiable> notifiables;

public Notifier (Set<? extends Notifiable> n) {
notifiables = n;
}

public void doNotifyAll(String message) {

Iterator<? extends Notifiable> i = notifiables.iterator();
while(i.hasNext()){
i.next().notify();
}


}
}

最佳答案

问题确实出在getPostgraduates()中。这样做的原因是,您只能与用空值初始化的对象 postgraduate 进行比较。你应该把整个学生群都跑一遍,研究生找你要找的导师去查。

关于java - 在集合中添加元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5356443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com