Copy ObservableList java

梦想与她 提交于 2019-12-05 01:24:15

Problem

public void setData(ObservableList<Foo> data) {
    this.data = data;
}

Explanation

In case you are wondering that Java obeys Pass By Value and the value of masterData should have been copied to the list data. You are correct ! But there is just a little twist to the story !

Incase of primitives, the values are passed but incase of objects, Object references are passed by value.

So, you are referencing to the Object of the masterData, instead of copying the values to a new List !

Solution

this.data = FXCollections.observableArrayList(data);

This will create a new Object for data to refer and all the manipulation's will be done on this new object's data.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!