Why SortedList.add() throws UnsupportedOperationException?

这一生的挚爱 提交于 2019-12-07 04:18:53

问题


Very simple code:

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 );
   }
}

Execution:

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)

回答1:


A SortedList is a sorted view of its underlying list. If you were allowed to add elements to the sorted list it would break that relationship. You need to add the element to the underlying list instead:

il.add(12);


来源:https://stackoverflow.com/questions/27804532/why-sortedlist-add-throws-unsupportedoperationexception

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