简体   繁体   English

添加到ArrayList后的日期更改

[英]Date changes after added to ArrayList

I have the following: 我有以下几点:

List<ArrayList<AvailablePeriodObj>> outputLists = new ArayList<ArrayList<AvailablePeriodObj>>();

ArrayList<AvailablePeriodObj> partOutputList = new ArrayList<AvailablePeriodObj>();    
DateTime beginCheck = periodsList.get(0).getStart();
DateTime endCheck = periodsList.get(0).getEnd();

for(int i= 0; i< periodsList.size(); i++) {
            AvailablePeriodObj apo = periodsList.get(i);

            if(apo.getStart().equals(beginCheck) && apo.getEnd().equals(endCheck)) { 
                partOutputList.add(apo);
            } else {
                outputLists.add(partOutputList);
                partOutputList.clear();
                beginCheck = apo.getStart();
                endCheck = apo.getEnd();
                partOutputList.add(apo);
            }
            if(i== (periodsList.size() - 1)){
                outputLists.add(partOutputList);
            }

        }

With this I am making new lists for all pairs of start and date, my problem is that after the process is completed every object in all the lists get the last used start and date value. 这样我就为所有的开始和日期对创建了新列表,我的问题是,在处理完成之后,所有列表中的每个对象都获得了上次使用的开始和日期值。

What I want as result is multiple ArraLists depending on how many different start-date pairs there are. 结果我想要的是多个ArraList,具体取决于有多少个不同的开始日期对。

Do not call partOutputList.clear(); 不要调用partOutputList.clear(); create a new partOutputList instance ( partOutputList = new ArrayList<AvailablePeriodObj>() ). 创建一个新的partOutputList实例( partOutputList = new ArrayList<AvailablePeriodObj>() )。

All you are doing right now is adding the same partOutputList instance to outputLists , clearing it then adding the last values. 您现在要做的就是将相同的partOutputList实例添加到outputLists ,清除它,然后添加最后一个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM