简体   繁体   English

ArrayList对象存储意外值

[英]ArrayList objects storing unexpected values

I have a class DailyDetails.java 我有一个类DailyDetails.java

DailyDetails.java: DailyDetails.java:

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

I have an ArrayList (named tempDD ). 我有一个ArrayList(名为tempDD )。 List<DailyDetails> tempDD = new ArrayList<>(); I want to add objects to this arrayList with subsequent calendar values (See the code). 我想将带有后续日历值的对象添加到此arrayList(请参见代码)。 lastAppOpenedDate and now are two instances of Calendar class. lastAppOpenedDatenow是Calendar类的两个实例。 Below is the code for adding objects to arrayList in my MainActivity. 下面是在MainActivity中将对象添加到arrayList的代码。

MainActivity.java 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. dailyDetails.calendar是关注点。

Expected results: Every time the loop runs, one minute is added to lastAppOpenedDate calendar value. 预期的结果:每次循环运行时,一分钟会添加到lastAppOpenedDate日历值中。 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.. 因此,假设arrayList tempDD中的第一个对象的分钟值为5,则下一个对象的分钟值为6,7,8,9,10,11,依此类推。

But in the actual output, the minute value in calendar in each object of arrayList is same. 但是在实际输出中,arrayList每个对象中日历中的分钟值都相同。 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. 这将在日志中打印12,12,12,12,12,12,12....。

Any help would be appreciated. 任何帮助,将不胜感激。 :) :)

You do instantiate a new DailyDetails object on each iteration of the loop. 您确实在循环的每次迭代上实例化了一个新的DailyDetails对象。 But you assign to its .calender attribute always a reference - not a copy - of lastAppOpenedDate . 但是,分配给它的.calender属性总是引用-不是一个副本-的lastAppOpenedDate

So you get a list of a dozen or so DailyDetails all referring to the identical instance stored in lastAppOpenedDate . 因此,您将获得十二打左右的DailyDetails列表,这些列表都引用了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. 每次您在for-i循环中访问该实例时,该实例自然会具有相同的值,该实例已更新了大约12次。

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

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

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