I want to do the following:
I have a List with Objects, which contain a Date, now I want to use this objects not only for one Date, I want to set the date and reuse them. Selected List contains 5 elements.
while((stop.equals(countingDate)) || (countingDate.before(stop))) {
for (c_TakeTimeObjects c_takeTimeObjects : SelectedList) {
c_TakeTimeObjects addingObj = new c_TakeTimeObjects(1,1,"",true,this);
c_takeTimeObjects.setsActivityDate(c_HelperClass_CalToStringDate.get(countingDate, 0, 0, 0));
addingObj = c_takeTimeObjects;
Log.i(TAG, "adjustDateRange: "+ addingObj.getsActivityDate());
editList.add(addingObj);
}
countingDate.add(Calendar.DAY_OF_MONTH, +1);
}
as you probably guess, the Log shows different dates caused by the loop, but the final list only has elements with the same Date.
log.i gives me:
adjustDateRange: 25.06.2019
adjustDateRange: 25.06.2019
adjustDateRange: 25.06.2019
adjustDateRange: 25.06.2019
adjustDateRange: 25.06.2019
adjustDateRange: 26.06.2019
adjustDateRange: 26.06.2019
adjustDateRange: 26.06.2019
adjustDateRange: 26.06.2019
adjustDateRange: 26.06.2019
adjustDateRange: 27.06.2019
adjustDateRange: 27.06.2019
adjustDateRange: 27.06.2019
adjustDateRange: 27.06.2019
adjustDateRange: 27.06.2019
adjustDateRange: 28.06.2019
adjustDateRange: 28.06.2019
adjustDateRange: 28.06.2019
adjustDateRange: 28.06.2019
adjustDateRange: 28.06.2019
and iterating through the List, after all objects are added gives me:
for(c_TakeTimeObjects c_takeTimeObjects: editList){
Log.i(TAG, "EditList: " + c_takeTimeObjects.getsActivityDate());
}
EditList: 28.06.2019
EditList: 28.06.2019
EditList: 28.06.2019
EditList: 28.06.2019
EditList: 28.06.2019
EditList: 28.06.2019
EditList: 28.06.2019
EditList: 28.06.2019
EditList: 28.06.2019
EditList: 28.06.2019
EditList: 28.06.2019
EditList: 28.06.2019
EditList: 28.06.2019
EditList: 28.06.2019
EditList: 28.06.2019
EditList: 28.06.2019
EditList: 28.06.2019
EditList: 28.06.2019
EditList: 28.06.2019
EditList: 28.06.2019
This is, because there is a reference between the objects I add, how can I avoid this? Maybe one solution is a DeepCopy, but is this really necessary?
Update
This Code produces the same result:
while((stop.equals(countingDate)) || (countingDate.before(stop))) {
for (c_TakeTimeObjects c_takeTimeObjects : SelectedList) {
c_takeTimeObjects.setsActivityDate(c_HelperClass_CalToStringDate.get(countingDate,0,0,0));
Log.i(TAG, "adjustDateRange: "+ c_takeTimeObjects.getsActivityDate());
editList.add(c_takeTimeObjects);
}
countingDate.add(Calendar.DAY_OF_MONTH, +1);
}