Java 创建一个 Observable 列表/集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26195243/
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-08-11 02:00:23  来源:igfitidea点击:

Creating an Observable List/Collection

javalistcollectionsinterfacejavafx

提问by LilSweden

I'm trying to create a ChoiceBoxin JavaFX 8, which requires a Collection. I can't figure out how to create a Collectionthough... If I try:

我正在尝试ChoiceBox在 JavaFX 8 中创建一个,它需要一个Collection. 我不知道如何创建一个Collection...如果我尝试:

 ObservableList<String> list = new ObservableList<String>();

I get an error saying I can't instantiate ObservableListbecause it's abstract. Understandable. If I look at the doc for ObservableListI can see that SortedList implements ObservableList, but I can't do:

我收到一个错误,说我无法实例化,ObservableList因为它是抽象的。可以理解。如果我查看文档,ObservableList我可以看到SortedList implements ObservableList,但我不能这样做:

 ObservableList<String> list = new SortedList<String>();

Because there's no applicable constructor. Apparently I need to have an ObservableListto pass to the SortedList, which is odd because I can't create an ObservableList.

因为没有适用的构造函数。显然我需要有一个ObservableList传递给SortedList,这很奇怪,因为我无法创建ObservableList.

constructor SortedList.SortedList(ObservableList<? extends String>,Comparator<? super String>) is not applicable
  (actual and formal argument lists differ in length)
constructor SortedList.SortedList(ObservableList<? extends String>) is not applicable
  (actual and formal argument lists differ in length)

I'm not sure how to decipher that. If I try

我不知道如何破译。如果我尝试

 ObservableList<String> list = new SortedList<SortedList<String>>();
 //or
 ObservableList<String> list = new SortedList<ObservableList<String>>();

out of desperation, I get an even more convoluted error.

出于绝望,我得到了一个更令人费解的错误。

    SortedList<String> list = new SortedList<String>();

doesn't work either. Somehow this works (but apparently uses an unsafe operation):

也不起作用。不知何故这是有效的(但显然使用了不安全的操作):

ChoiceBox box = new ChoiceBox(FXCollections.observableArrayList("Asparagus", "Beans", "Broccoli", "Cabbage" , "Carrot", "Celery", "Cucumber", "Leek", "Mushroom" , "Pepper", "Radish", "Shallot", "Spinach", "Swede" , "Turnip"));

So I tried:

所以我试过:

 ObservableList<string> list = new FXCollections.observableArrayList("Asparagus", "Beans", "Broccoli", "Cabbage" , "Carrot", "Celery", "Cucumber", "Leek", "Mushroom" , "Pepper", "Radish", "Shallot", "Spinach", "Swede" , "Turnip");

But no luck there either. I'm super confused, doing the same thigns over and over in an endless loop of trying to understand this. The documentation I've found shows examples that don't help, or no examples. The official documentation is useless too:

但也没有运气。我非常困惑,在试图理解这一点的无限循环中一遍又一遍地做同样的事情。我找到的文档显示了没有帮助的示例,或者没有示例。官方文档也没有用:

Suppose, for example, that you have a Collection c, which may be a List, a Set, or another kind of Collection. This idiom creates a new ArrayList (an implementation of the List interface), initially containing all the elements in c.

 List<String> list = new ArrayList<String>(c);

例如,假设您有一个集合 c,它可能是一个列表、一个集合或其他类型的集合。这个习惯用法创建了一个新的 ArrayList(List 接口的实现),最初包含 c 中的所有元素。

 List<String> list = new ArrayList<String>(c);

So to create ArrayList, an implementation of List, I need to have a List. the reason I went to the documentation in the first place was to learn how to make what they're assuming I have. I'm lost. Help?

所以要创建ArrayList一个 的实现List,我需要有一个List. 我首先查看文档的原因是学习如何制作他们假设我拥有的东西。我迷路了。帮助?

采纳答案by James_D

Use the factory methods in FXCollections:

使用工厂方法FXCollections

ObservableList<String> list = FXCollections.observableArrayList();

The unsafe operation in your choice box constructor is because you haven't specified the type for the choice box:

您的选择框构造函数中的不安全操作是因为您没有为选择框指定类型:

ChoiceBox<String> box = new ChoiceBox<>(FXCollections.observableArrayList("Asparagus", "Beans", "Broccoli", "Cabbage" , "Carrot", "Celery", "Cucumber", "Leek", "Mushroom" , "Pepper", "Radish", "Shallot", "Spinach", "Swede" , "Turnip"));

and the error from SortedListis because there is no constructor taking no arguments. (Again, refer to the javadocs.) There are two constructors: the simplest one takes a reference to an ObservableList(the list for which the sorted list will provide a sorted view). So you would need something like

错误SortedList是因为没有不带参数的构造函数。(同样,请参阅javadocs。)有两个构造函数:最简单的一个引用 an ObservableList(排序列表将为其提供排序视图的列表)。所以你需要类似的东西

SortedList<String> sortedList = new SortedList<>(list);

or

或者

SortedList<String> sortedList = new SortedList<>(FXCollections.observableArrayList());