java ArrayList 和 ObservableList 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41920217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:14:39  来源:igfitidea点击:

What is the difference between ArrayList and ObservableList?

javajavafx-2

提问by Matiullah Karimi

Currently I am working with database on javafx and have no idea about ObservableList, can I use ArrayList instead of ObservableList?

目前我正在使用 javafx 上的数据库并且不知道 ObservableList,我可以使用 ArrayList 而不是 ObservableList 吗?

回答by fabian

That depends. If you need a ObservableList, you cannot use ArrayListdirectly. ObservableListadds a way to listen for changes on a list which ArrayListdoes not implement.

那要看。如果需要ObservableList,则不能ArrayList直接使用。ObservableList添加了一种侦听ArrayList未实现列表更改的方法。

However you could use a ArrayListas backing list of a ObservableList

但是,您可以使用 aArrayList作为a 的支持列表ObservableList

ArrayList<T> list = ...
ObservableList<T> observableList = FXCollections.observableList(list);

Note that in this case you should make sure you're not modifying the list through any means but observableList, since otherwise the listeners won't be triggered.

请注意,在这种情况下,您应该确保您没有通过任何方式修改列表,但是observableList,否则将不会触发侦听器。

Note that FXCollectionsalso provides a method for creating a ObservableListbacked by a ArrayListwithout having to deal with the ArrayListitself:

请注意,FXCollections它还提供了ObservableList一种ArrayList无需处理ArrayList自身即可创建由 a 支持的方法:

ObservableList<T> observableList = FXCollections.observableArrayList();

回答by Monzurul Haque Shimul

ArrayList:Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null.

ArrayList:List 接口的可调整大小的数组实现。实现所有可选的列表操作,并允许所有元素,包括 null。

ObservableList:A list that allows listeners to track changes when they occur.

ObservableList:允许侦听器在发生更改时跟踪更改的列表。

回答by J. Markiewicz

It depends of your case. If you want to show this list in for example tableView or other view then you should use Observable collection which contains listeners ect and other components necessery for doing interaction with view.

这取决于你的情况。如果你想在例如 tableView 或其他视图中显示这个列表,那么你应该使用 Observable 集合,其中包含与视图交互所必需的侦听器等和其他组件。