无法在JavaFX中向ObservableList(UnsupportedOperationException)添加元素

时间:2016-07-04 11:18:34

标签: java arrays list javafx

初始起点

我有1000个ListPerson个对象,我想插入Extractor来监听任何Person个对象中的属性更改({{1}稍后将附加到ObservableList)。

所以我的代码就像:

TableView

错误消息

但是当我尝试向这个ObservableList observablePersons 添加一个新人对象时,我遇到了这个错误:

ObservableList<Person> observablePersons = FXCollections.observableList(personlist,
    personextractor);

您能否告诉我为什么会遇到此错误消息?我的java版本是 jdk1.8.0_91(32位)

人员类

run:
Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(AbstractList.java:148)
    at com.sun.javafx.collections.ObservableListWrapper.doAdd(ObservableListWrapper.java:101)
    at javafx.collections.ModifiableObservableListBase.add(ModifiableObservableListBase.java:151)
    at java.util.AbstractList.add(AbstractList.java:108)
    at test.listchangelistener.listChangeDemo.main(listChangeDemo.java:72)
Java Result: 1

测试代码

package test.listchangelistener;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

class Person {

    private final IntegerProperty age = new SimpleIntegerProperty();
    private final StringProperty name = new SimpleStringProperty();

    public Person(String name, Integer age) {
        setName(name);
        setAge(age);
    }

    public int getAge() {
        return age.get();
    }

    public final void setAge(int value) {
        age.set(value);
    }

    public IntegerProperty ageProperty() {
        return age;
    }

    public String getName() {
        return name.get();
    }

    public final void setName(String value) {
        name.set(value);
    }

    public StringProperty nameProperty() {
        return name;
    }

    @Override
    public String toString() {
        return "Person{" + "age=" + age.get() + ", name=" + name.get() + '}';
    }

}

1 个答案:

答案 0 :(得分:12)

问题来自创建支持List

List<Person> persons = Arrays.asList(p1, p2);

如果您查看javadoc of Arrays.asList

  

返回由指定数组支持的固定大小的列表。

仔细看看你得到UnsupportedOperationException,因为List.add()可选操作

  

抛出:       UnsupportedOperationException - 如果此列表不支持添加操作。

您可以将支持列表的创建更新为:

List<Person> persons = new ArrayList<Person>(Arrays.asList(p1, p2));

不同之处在于返回的ArrayList是传递的#include "tiffio.h" #include <iostream> using namespace std; int main(void) { TIFF *tif = TIFFOpen("8bitRaw.dng", "w"); const int sampleperpixel = 1; const int width = 4; const int height = 4; static const short bayerPatternDimensions[] = { 2,2 }; static const double cam_xyz[] = { 2.0413690, -0.5649464, -0.3446944, -0.9692660, 1.8760108, 0.0415560,0.0134474, -0.1183897, 1.0154096 }; //AdobeRGB static const double neutral[] = { 1.0, 1.0, 1.0 }; long max_white = 255; long sub_offset = 0; int row, i; float gamma = 80; //Arbitrary Bayer pixel values: unsigned char image[height][width*sampleperpixel] = {}; image[0][0] = 5; image[0][1] = 165; image[0][2] = 0; image[0][3] = 255; image[1][0] = 0; image[1][1] = 21; image[1][2] = 0; image[1][3] = 10; image[2][0] = 0; image[2][1] = 0; image[2][2] = 30; image[2][3] = 5; image[3][0] = 21; image[3][1] = 120; image[3][2] = 1; image[3][3] = 254; //Black Thumbnail pixel values: unsigned char Thumbnail_RGB_Array[] = { 0,0,0,0,0,0,0,0,0,0,0,0 }; //Linearization Table: unsigned short curve[256]; for (i = 0; i < 256; i++) curve[i] = 255 * pow(i / 255.0, 100.0 / gamma) + 0.5; TIFFSetField(tif, TIFFTAG_SUBFILETYPE, 1); TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, 4); TIFFSetField(tif, TIFFTAG_IMAGELENGTH, 4); TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3); TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE); TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); TIFFSetField(tif, TIFFTAG_XRESOLUTION, 75.0); TIFFSetField(tif, TIFFTAG_YRESOLUTION, 75.0); TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH); TIFFSetField(tif, TIFFTAG_MAKE, "DummyMake"); TIFFSetField(tif, TIFFTAG_MODEL, "DummyModel"); TIFFSetField(tif, TIFFTAG_SOFTWARE, "DummySoftware"); TIFFSetField(tif, TIFFTAG_ORIGINALRAWFILENAME, 1, "DummyName.dng"); TIFFSetField(tif, TIFFTAG_UNIQUECAMERAMODEL, "DummyUniqueModel"); TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, "DummyImageDescription"); TIFFSetField(tif, TIFFTAG_COPYRIGHT, "DummyCopyright"); TIFFSetField(tif, TIFFTAG_DATETIME, "2016:06:30 11:11:15"); TIFFSetField(tif, TIFFTAG_DNGVERSION, "\01\01\00\00"); TIFFSetField(tif, TIFFTAG_DNGBACKWARDVERSION, "\01\00\00\00"); TIFFSetField(tif, TIFFTAG_COLORMATRIX1, 9, cam_xyz); TIFFSetField(tif, TIFFTAG_ASSHOTNEUTRAL, 3, neutral); TIFFSetField(tif, TIFFTAG_CALIBRATIONILLUMINANT1, 21); //SensingMethodTag and TIFF/EPStandardID tag: Libtiff doesn't seem to know these: //TIFFSetField(tif, 37399, 2); //TIFFSetField(tif, 37398, "\01\01\00\00"); //Yields an error: //TIFFSetField(tif, TIFFTAG_SUBIFD, 1, &sub_offset); //Write a black 4x4 pixel dummy thumbnail: for (row = 0; row < 4 ; row++) TIFFWriteScanline(tif, Thumbnail_RGB_Array, row, 0); TIFFWriteDirectory(tif); //Now write main raw image: TIFFSetField(tif, TIFFTAG_SUBFILETYPE, 0); TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width); TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height); TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_CFA); TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1); TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(tif, TIFFTAG_CFAREPEATPATTERNDIM, bayerPatternDimensions); TIFFSetField(tif, TIFFTAG_CFAPATTERN, "\01\00\02\01"); TIFFSetField(tif, TIFFTAG_CFAPLANECOLOR, 3, "\00\01\02"); TIFFSetField(tif, TIFFTAG_LINEARIZATIONTABLE, 256, curve); TIFFSetField(tif, TIFFTAG_WHITELEVEL, 1, &max_white); TIFFWriteScanline(tif, image[0], 0, 0); TIFFWriteScanline(tif, image[1], 1, 0); TIFFWriteScanline(tif, image[2], 2, 0); TIFFWriteScanline(tif, image[3], 3, 0); TIFFClose(tif); return 0; } 的完全独立副本,因此在这种情况下上述限制不再有效。

您还可以查看这个问题,深入解释这个主题:

Difference between Arrays.asList(array) vs new ArrayList<Integer>(Arrays.asList(ia)) in java