I put increment date of the calendar and add into arraylist, but when i loop them out, the date is all same.
Calendar a = Calendar.getInstance();
ArrayList<Calendar> b = new ArrayList<Calendar>();
for (int i=0; i<3; i++) {
a.add(Calendar.DATE, i>0 ? 1 : 0);
b.add(a);
}
for (int i=0; i<b.size(); i++){
Log.d("xxx", "test=" + b.get(i));
}
suppose i wan 10/11/2012, 11/11/2012, 12/11/2012
but it come out like 12/11/2012, 12/11/2012, 12/11/2012
Anyone know how to solve this?
The problem is that you are adding the same instance of
Calendarto yourArrayListso when you go to print it out all the dates will be the same.One solution would be to clone your
Calendarobjects, giving you different date values:Consider instead, storing the dates as strings here though as this would be less of an overhead rather than creating multiple
Calendarobjects.