简体   繁体   中英

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.

Do not call partOutputList.clear(); create a new partOutputList instance ( 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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