为什么 SortedList.add() 抛出 UnsupportedOperationException?
Why SortedList.add() throws UnsupportedOperationException?
非常简单的代码:
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.SortedList;
public final class SortedListTest {
public static void main( String[] args ) {
final ObservableList<Integer> il = FXCollections.observableArrayList();
final SortedList<Integer> sil = new SortedList<>( il );
sil.comparatorProperty().set((l,r)-> l-r );
sil.add( 12 );
}
}
执行:
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
at java.util.AbstractList.add(AbstractList.java:108)
at SortedListTest.main(SortedListTest.java:13)
A SortedList
是其基础列表的排序视图。如果您被允许向排序列表中添加元素,则会破坏这种关系。您需要将元素添加到基础列表中:
il.add(12);
非常简单的代码:
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.SortedList;
public final class SortedListTest {
public static void main( String[] args ) {
final ObservableList<Integer> il = FXCollections.observableArrayList();
final SortedList<Integer> sil = new SortedList<>( il );
sil.comparatorProperty().set((l,r)-> l-r );
sil.add( 12 );
}
}
执行:
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
at java.util.AbstractList.add(AbstractList.java:108)
at SortedListTest.main(SortedListTest.java:13)
A SortedList
是其基础列表的排序视图。如果您被允许向排序列表中添加元素,则会破坏这种关系。您需要将元素添加到基础列表中:
il.add(12);