简体   繁体   English

使用 toString 拆分 observableList

[英]Splitting an observableList using toString

So I am trying to split an observableList using toString:所以我正在尝试使用 toString 拆分 observableList:

listeMedLag = DataHandler.hentDataCupLag("src/arrangementer.csv",2,listeMedLag,cup); //output: Skjeberg&Borgen
listeMedLag.toString().split("&");
lagSomErMed.setItems(listeMedLag); //output: Skjeberg&Borgen

Anybody know why the list ain't splitting?有人知道为什么列表没有拆分吗?

listeMedLag.toString()

Creates a string based on the elements of the list.根据列表的元素创建一个字符串。 There are multiple problems with that.这有很多问题。 The ObservableList implementation may not create exactly the string output you expect. ObservableList实现可能不会完全创建您期望的字符串 output。 Also usually there are {} wrapping the content.通常还有{}包装内容。

Also invoking methods on objects returned by methods of an object usually won't have an effect on the original object.此外,对由 object 的方法返回的对象调用方法通常不会对原始 object 产生影响。 There are exceptions, but they are documented.有例外,但它们已记录在案。 (Examples would be List.subList and List.iterator ). (例如List.subListList.iterator )。 String is immutable though and it has no connection to the list whatsoever.不过, String是不可变的,它与列表没有任何关系。 Calling split on the string simply results in an new String[] array being created that contains parts of the string.对字符串调用split只会导致创建一个包含部分字符串的新String[]数组。 It has no effect on the string object and also no effect on the list.它对字符串 object 没有影响,对列表也没有影响。

If your intention is to split every element of a List<String> and fill a list with all those elements I recommend you create a new list containg all those elements and then assign the contents to the target list:如果您打算拆分List<String>的每个元素并用所有这些元素填充列表,我建议您创建一个包含所有这些元素的新列表,然后将内容分配给目标列表:

List<String> inputList = ...
ObservableList<String> outputList = FXCollections.observableArrayList();

// fill new list with elements of original list split at "&"
inputList.stream().map(s -> s.split("&")).forEach(outputList::addAll);

// do something with new list
listView.setItems(outputList);

You need to store that splitted string into a local variable.您需要将该拆分的字符串存储到局部变量中。 like:喜欢:

String[] split =  = listeMedLag.toString().split("&");
lagSomErMed.setItems(split);

or或者

lagSomErMed.setItems(listeMedLag.toString().split("&"));

split method signature: split方法签名:

public String[] split(String regex)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM