0

So basically I have code that looks like this

supersetRowModel.setExerciseModelList(exerciseModelList);
exerciseModelList.clear();

My getter and setter for this class

 public void setExerciseModelList(List<ExerciseModel> exerciseModelList){
    this.exerciseModelList = exerciseModelList;
}

public List<ExerciseModel> getExerciseModelList(){
    return exerciseModelList;
}

Whenever I call getExerciseModelList() right after I clear the array list, it returns a list with size 0. I have checked and made sure that exerciseModelList is not size 0. This is confusing me, since clearing exerciseModelList should not affect the objects arraylist since it is already set, right?

4
  • Just do exerciseModelList = new ArrayList<>(); instead of clearing it. Commented Dec 6, 2017 at 4:03
  • Works, thanks. But why wouldn't my method work? Commented Dec 6, 2017 at 4:07
  • Because your assumption is wrong. Both references are pointing to the same list. Commented Dec 6, 2017 at 4:08
  • That makes sense now. Commented Dec 6, 2017 at 4:11

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.