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

标签 java

我有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/

相关文章:

java - Thymeleaf请求参数空值问题

java - Spring 表达式语言和 Spring Security 3 : accessing bean reference in @PreAuthorize

java - Spring Boot 2.7 EOL 后的缓解措施

java - 面向对象编程 - 避免 Switch/Case 和 If/else (JAVA)

java - 增加 Ant 的内存力

Java - 停止具有共享对象的线程

java - 具有返回类型的消费者接口(interface)的 lambda 表达式

Java Spring PropertyPlaceholderConfigurer 以及如何修剪属性文件中的空格/制表符

java - JAI.create 中的 DPI 变化...为什么?

java - executeQuery() 返回空结果集