简体   繁体   中英

ArrayList objects storing unexpected values

I have a class DailyDetails.java

DailyDetails.java:

public class DailyDetails implements Serializable {
    Calendar calendar;
    int volume;
    int target;
}

I have an ArrayList (named tempDD ). List<DailyDetails> tempDD = new ArrayList<>(); I want to add objects to this arrayList with subsequent calendar values (See the code). lastAppOpenedDate and now are two instances of Calendar class. Below is the code for adding objects to arrayList in my MainActivity.

MainActivity.java

while(lastAppOpenedDate.get(Calendar.MINUTE) != now.get(Calendar.MINUTE)) {
            DailyDetails dailyDetails = new DailyDetails();
            dailyDetails.volume = waterConsumed; 
            preferences =
            PreferenceManager.getDefaultSharedPreferences(this);
            if(preferences.getBoolean("default_target",true)) {
                dailyDetails.target =
                preferences.getInt("defaultTargetValue",4000);
            }
            else {
                dailyDetails.target =
                preferences.getInt("customTargetValue",4000);
            }
            dailyDetails.calendar = lastAppOpenedDate; // Adding the calendar value
            tempDD.add(dailyDetails); //Adding object to ArrayList.
            if(tempDD.size() > 30 || tempDD.get(0).calendar == tempDD.get(1).calendar) { // Can be ignored right now. 
                tempDD.remove(0);
            }
            lastAppOpenedDate.add(Calendar.MINUTE, 1); // Adding one MINUTE to lastAppOpenedDate
            waterConsumed = 0;
        }

dailyDetails.calendar is the point of concern.

Expected results: Every time the loop runs, one minute is added to lastAppOpenedDate calendar value. therefore, Suppose first object in arrayList tempDD has minute value 5 then the next objects should have minute values 6,7,8,9,10,11 and so on..

But in the actual output, the minute value in calendar in each object of arrayList is same. ie I added this code after the above code:

String s = "";
for(int i = 0; i < tempDD.size();i++){
    s = s + tempDD.get(i).calendar.get(Calendar.MINUTE) + ",";
}
Log.d("Tag A","String: "+s);

This will print 12,12,12,12,12,12,12.... in the Log.

Any help would be appreciated. :)

You do instantiate a new DailyDetails object on each iteration of the loop. But you assign to its .calender attribute always a reference - not a copy - of lastAppOpenedDate .

So you get a list of a dozen or so DailyDetails all referring to the identical instance stored in lastAppOpenedDate . And that one instance will naturally have the same value everytime you access it in the for-i loop, which you have updated for about a dozen times.

Change dailyDetails.calendar = lastAppOpenedDate; to dailyDetails.calendar=lastAppOpenedDate.clone();

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