なぜDefaultListModel.toArrayは()にClassCastExceptionをスローしていますか?

テッドのpottel:

私は、コピーしようとしているDefaultListModel配列に内容を。次の行は、例外が発生します

testArray =(cGenIndicator [])indObjList.toArray();

void testCasting() {
    DefaultListModel<cGenIndicator> indObjList;
    indObjList = new DefaultListModel<cGenIndicator>();
    indObjList.addElement(new cGenIndicator(null, null));

    cGenIndicator[] testArray;
    try {
        // This line causses exception saying
        // [Ljava.lang.Object; cannot be cast to [LIndicator.cGenIndicator;
        testArray = (cGenIndicator[]) indObjList.toArray();
    } catch(Exception e) {
        test++;
    }

    test++;
}
xingbin:

DefaultListModel.toArrayリターンはObject[]、とObject[]にキャストすることはできませんcGenIndicator[]直接。

あなたは、このようにそれを達成することができます:

Object[] objectArray = defaultListModel.toArray();
int length = objectArray.length;

cGenIndicator[] testArray = new cGenIndicator[length];
System.arraycopy(objects, 0, testArray, 0, length);

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=222443&siteId=1