Search code examples
javaandroidstatic-methodsandroid-livedatautility

Static method changing value of the variable used as an argument


I have created a static method in a PredictionUtil class which is used to generate a new list from a smaller list. Instead of just being assigned to the new variable, the new updated list is also assigned to the smaller list. I am unable to understand this problem.

The utility class:

public class PredictionUtils {

    public static List<PeriodEntity> predictPeriods(List<PeriodEntity> past){
        List<PeriodEntity> updated = past;
        if(past == null){
            return null;
        }
        int pastsize = past.size();
        long currentcycledur = generateCurrentCycleDuration(past);
        long currentperioddur = generateCurrentPeriodDuration(past);
        long laststartdate = past.get(pastsize-1).getStartTimestamp();
        for(int i = 1;i <= 20;i++){
            updated.add(new PeriodEntity(laststartdate+(currentcycledur*i),laststartdate+(currentcycledur*i)+currentperioddur));
        }
        return updated;
    }

    public static long generateCurrentCycleDuration(List<PeriodEntity> past){
        long cycleduration = 0L;
        long diff;
        int pastsize = past.size();

        if(pastsize == 1){
            return cycleduration = 2419200000L;
        }

        if(pastsize < 5) {
            for (int i = 0; i < pastsize - 1; i++) {
                diff = past.get(i + 1).getStartTimestamp() - past.get(i).getStartTimestamp();
                cycleduration += diff;
            }
            return (cycleduration/(pastsize-1));
        }else{
            for (int i = pastsize-1;i >= pastsize-4;i--) {
                diff = past.get(i).getStartTimestamp() - past.get(i-1).getStartTimestamp();
                cycleduration += diff;
            }
            return (cycleduration/4);
        }
    }

    public static long generateCurrentPeriodDuration(List<PeriodEntity> past){
        long periodduration = 0L;
        long diff;
        int pastsize = past.size();

        if(pastsize < 4){
            for(int i = 0;i < pastsize;i++){
                diff = past.get(i).getEndTimestamp()-past.get(i).getStartTimestamp();
                periodduration += diff;
            }
            return (periodduration/pastsize);
        }else{
            for(int i = pastsize-1;i >= pastsize-4;i--){
                diff = past.get(i).getEndTimestamp()-past.get(i).getStartTimestamp();
                periodduration += diff;
            }
            return (periodduration/4);
        }
    }
}

Its use:

//Observable LiveData Elements
        calendarViewModel.getPastPeriods().observe(getViewLifecycleOwner(), new Observer<List<PeriodEntity>>() {
            @Override
            public void onChanged(final List<PeriodEntity> pastperiods) {

                nextPeriod = PredictionUtils.predictPeriods(pastperiods).get(pastperiods.size());

            }
        });

Value of the pastperiods and nextPeriod is same, why?


Solution

  • Instead of creating a new instance of List, you are referring to the input.

    List<PeriodEntity> updated = past;
    

    As a result, both updated and past lists are basically the same list. So whenever you are modifying updated, it also updates the past.

    To avoid that, create a new ArrayList for updated and copy the existing data of past there.

    List<PeriodEntity> updated = new ArrayList<>(past);