Java-如何将项目从ArrayList中的一个类传递到构造函数// getter和setter中的另一个?

时间:2018-08-28 05:52:48

标签: java arraylist constructor model

想要一些有关如何从ArrayList访问项目并将其传递给构造函数的建议。

我有这段代码可以生成并存储ArrayListsInteger中的两个Date

ArrayList <Date> days = new ArrayList<>();
Integer[] count = {0,0,0,0,0,0}; //Use Integer not int
List<Integer> recordCount = Arrays.asList(count);

for(int x = 0; x < 7; x++) {
    days.add(dateUtils.stringToDateWithTime(lastWeek.toString()));                     
    calendar.add(Calendar.DAY_OF_MONTH, 1);
}

for(int x = 0; x < 7; x++) {
    for(int y = 0; y < r.size(); y++) {
        if (!(days.get(x) == r.get(y).getDate())) {
            int oldValue = recordCount.get(x);
            int newValue = oldValue + 1;
            recordCount.set(x, newValue);
        }
    }
}
dataModel.setyValueCount(what do i put in here?);
dataModel.setxValueDate();

我想将它们传递给的是我的DataModel类:

public class DataModel {

    private long yValueCount;
    private Date xValueDate;

    private String someStringField;
    private String label;

    public DataModel() {

    }

    public DataModel(Date xValueDate, int yValueCount) {
        this.xValueDate = xValueDate;
        this.yValueCount = yValueCount;
    }

    public long getyValueCount() {
        return yValueCount;
    }

    public void setyValueCount(long yValueCount) {
        this.yValueCount = yValueCount;

    }

    public Date getxValueDate() {
        return xValueDate;
    }

    public void setxValueDate(Date xValueDate) {
        this.xValueDate = xValueDate;
    }

}

此数据将插入到将在应用程序中显示的图表中。谢谢。

1 个答案:

答案 0 :(得分:0)

for(int x = 0;x<7;x++)
{
    for (int y = 0; y < r.size(); y++) {
           //create object here
           DataModel dataModel=new DataModel();
        if (!(days.get(x) == r.get(y).getDate())) {
            int oldValue = recordCount.get(x);
            int newValue = oldValue + 1;
            recordCount.set(x, newValue);
            //set here what do you want
            dataModel.setyValueCount(long type);
            dataModel.setxValueDate(Date type);
           //use  dataModel object for any other process like persist
          //looking for this or any other..........?
        }
    }
}