gpt4 book ai didi

java - 通过在java中放入列表来设置和获取数据循环

转载 作者:行者123 更新时间:2023-12-02 13:11:53 25 4
gpt4 key购买 nike

我有for循环,我可以在其中获取数据发送到不同类中的其他方法

UserManagerImpl.java

for (User user: userList) {
surveyModel survey = new SurveyModel();
survey.setSurveyData(Id, comment, serveyScore, grade);
}

在其他类中,我已经设置并获取创建数据列表,然后想通过 get 方法获取它。

调查模型.java

public class SurveySentimentModel {
public static String delimiter = "|||";
private List scores = new ArrayList();
private List negativeData = new ArrayList();
private List PositiveData = new ArrayList();


public void setSurveyData(String Id, String comment, double Score, String grade) {
//want to add score to scores list
//if grade positive add to positive list or else negative after delimiting
}
public double getTotalScore() {
//calculate the sum of scores
return totalScore;
}


public String getTotalSentimentgrade() {
if (totalScore> 0) {
return "Positive";
}
return "Negative";
}

public List getSurveyData() {
//Want to merge list - first negative and then positive
return new ArrayList();
}
}

SurveyModel.java

private String grade;
private Number Score;
private String Id;
private String comment;

public SurveyModel(String Id, String comment, double Score, String grade) {
this.grade= grade;
this.Score= Score;
this.comment = comment;
this.Id = Id;
}
public SurveyModel() {
// TODO Auto-generated constructor stub
}

//getter 和 setter

在这里我想要

1.) Add score to scores list 
2.) want to add graddes to list by negative first with delimiter then positive.
3.) want to get total score.

我怎样才能达到我的要求。我是java新手,请帮助我。

最佳答案

这里有一个建议:

这是模型类:

    public class SurveySentimentModel {
public static String delimiter = "|||";
private List<SurveyModel> scores = new ArrayList();
private List<SurveyModel> negativeData = new ArrayList();
private List<SurveyModel> positiveData = new ArrayList();


public void setSurveyData(String Id, String comment, double score, String grade) {
SurveyModel survey = new SurveyModel(id, comment, score, grade );
scores.add(survey)
if(score >= 0){
positiveData.add(survey);
}else{
negativeData.add(survey);
}
}
public double getTotalScore() {
double sum = 0;
for(SurveyModel s: scores){
sum += s.getScore();
}
return sum;
}
public List getSurveyData() {
List<SurveyModel> joined = new ArrayList(negativeData);
joined.addAll(positiveData)
return joined;
}
}

这是循环:

SurveySentimentModel sentiments = new SurveySentimentModel();
for (User user: userList) {
sentiments.setSurveyData(user.getId(), user.getComment(), user.getSurveryScore(), user.getGrade());
}

关于java - 通过在java中放入列表来设置和获取数据循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43922678/

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