Java 如何通过将列表的参数添加为对象来发送列表
我有一个带有对象参数的模型 如果有3个不同的候选者,它应该显示3个候选者,但我的输出将重复最后一个候选者的详细信息3次。我没有得到前两个候选人的详细信息Java 如何通过将列表的参数添加为对象来发送列表,java,spring-mvc,collections,Java,Spring Mvc,Collections,我有一个带有对象参数的模型 如果有3个不同的候选者,它应该显示3个候选者,但我的输出将重复最后一个候选者的详细信息3次。我没有得到前两个候选人的详细信息 public class CandidateFeedbackDisplay implements Serializable{ private Candidate candidate; private List<Integer> feedbackIds; //setter and getters } pub
public class CandidateFeedbackDisplay implements Serializable{
private Candidate candidate;
private List<Integer> feedbackIds;
//setter and getters
}
public List<CandidateFeedbackDisplay> list(Integer cID, Integer jID, String accepted) throws Exception {
Session session = this.sessionFactory.getCurrentSession();
List<Candidate> candidateList = null;
CandidateFeedbackDisplay feedbackDisplay = new CandidateFeedbackDisplay();
List<CandidateFeedbackDisplay> feedbackDisplayList = new ArrayList();
// List<CandidateFeedbackDisplay> feedbackDisplayListTest = null;
try {
Query query = session.createQuery("from Candidate WHERE phoneNumber IN (select DISTINCT mobileNo from InviteCandidates WHERE c_id= :cID AND j_id= :jID AND status= :accepted)");
query.setInteger("cID", cID);
query.setInteger("jID", jID);
query.setString("accepted", accepted);
candidateList = query.list();
Iterator itr = candidateList.iterator();
while(itr.hasNext()){
Candidate candidate = (Candidate) itr.next();
System.out.println("candidate.getCandidateID() : " + candidate.getCandidateID());
List<CandidateFeedback> candidateFeedback = this.getFeedback(cID, jID, candidate.getCandidateID());
Iterator itr1 = candidateFeedback.iterator();
List<Integer> feedbackid = new ArrayList<Integer>();
while(itr1.hasNext()){
CandidateFeedback Feedback = (CandidateFeedback) itr1.next();
feedbackid.add(Feedback.getFeedbackID());
}
feedbackDisplay.setFeedbackIds(feedbackid);
feedbackDisplay.setCandidate(candidate);
feedbackDisplayList.add(feedbackDisplay);
// feedbackDisplayListTest.add(feedbackDisplay); // null pointer access error
}
}catch (Exception e) {
e.printStackTrace();
this.logger.error("Error while fetching List :" + e);
return null;
}
return feedbackDisplayList;
}
将同一对象添加到列表中三次。每次都必须通过移动此行来创建新对象:
CandidateFeedbackDisplay feedbackDisplay = new CandidateFeedbackDisplay();
进入while循环,否则将继续更改先前放置的对象的属性。事实上,您正在更改同一对象,并将其添加到列表中三次
public List<CandidateFeedbackDisplay> list(Integer cID, Integer jID, String accepted) throws Exception {
...
// DELETE HERE
// CandidateFeedbackDisplay feedbackDisplay = new CandidateFeedbackDisplay();
List<CandidateFeedbackDisplay> feedbackDisplayList = new ArrayList();
try {
...
while(itr.hasNext()) {
...
// INSERT HERE
CandidateFeedbackDisplay feedbackDisplay = new CandidateFeedbackDisplay();
feedbackDisplay.setFeedbackIds(feedbackid);
feedbackDisplay.setCandidate(candidate);
feedbackDisplayList.add(feedbackDisplay);
}
} catch (Exception e) {
...
}
return feedbackDisplayList;
}
附加说明:为了防止下次出现此类错误,您可以将参数对象CandidateFeedbackDisplay从setters更改为基于构造函数的实现:
public class CandidateFeedbackDisplay {
private final Candidate candidate;
private final List<Integer> feedbackIds;
public CandidateFeedbackDisplay(Candidate candidate, List<Integer> feedbackIds) {
this.candidate = candidate;
this.feedbackIds = feedbackIds;
}
// no setters
// add getters or make fields public, but keep final
}
这样,您就可以真正证明这个对象只是一个不可变的值持有者。您不能再犯同样的错误,构造函数可能会将代码缩短一点。当然,优点和缺点取决于您的具体情况。放置候选反馈显示feedbackDisplay feedbackDisplay=new CandidateFeedbackDisplay;在while循环内,因为您正在尝试准备候选反馈显示列表
请查找下面的代码,希望对您有所帮助
while(itr.hasNext()){
CandidateFeedbackDisplay feedbackDisplay = new CandidateFeedbackDisplay();
Candidate candidate = (Candidate) itr.next();
System.out.println("candidate.getCandidateID() : " + candidate.getCandidateID());
List<CandidateFeedback> candidateFeedback = this.getFeedback(cID, jID, candidate.getCandidateID());
Iterator itr1 = candidateFeedback.iterator();
List<Integer> feedbackid = new ArrayList<Integer>();
while(itr1.hasNext()){
CandidateFeedback Feedback = (CandidateFeedback) itr1.next();
feedbackid.add(Feedback.getFeedbackID());
}
feedbackDisplay.setFeedbackIds(feedbackid);
feedbackDisplay.setCandidate(candidate);
feedbackDisplayList.add(feedbackDisplay);
}
首先,你需要解释你的代码,放上注释,指出你有问题的确切代码,并删除不必要的代码谢谢你的快速回复谢谢你。“你救了我的命。”普里亚当然,听到这个消息很高兴。我在结尾加了一个小纸条,这有助于防止将来发生这样的错误。谢谢你的回复