<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    kooyee ‘s blog

    開源軟件, 眾人努力的結晶, 全人類的共同財富
    posts - 103, comments - 55, trackbacks - 0, articles - 66
       :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理
    常常使用Arrays.asLisvt()后調用add,remove這些method時出現java.lang.UnsupportedOperationException異常。這是由于:

    Arrays.asLisvt() 返回java.util.Arrays$ArrayList, 而不是ArrayList。Arrays$ArrayList和ArrayList都是繼承AbstractList,remove,add等method在AbstractList中是默認throw UnsupportedOperationException而且不作任何操作。ArrayList override這些method來對list進行操作,但是Arrays$ArrayList沒有override remove(int),add(int)等,所以throw UnsupportedOperationException。

    解決方法是使用Iterator,或者轉換為ArrayList
    List list = Arrays.asList(a[]);
    List arrayList 
    = new ArrayList(list);


    參考

    When you call Arrays.asList it does not return a java.util.ArrayList. It returns a java.util.Arrays$ArrayList which is an immutable list. You cannot add to it and you cannot remove from it.

    If you want a mutable list built from your array you will have to loop over the array yourself and add each element into the list in turn.

    Even then your code won't work because you'll get an IndexOutOfBoundsException as you remove the elements from the list in the for loop. There are two options: use an Iterator which allows you to remove from the list as you iterate over it (my recommendation as it makes the code easier to maintain) or loop backwards over the loop removing from the last one downwards (harder to read).

    You are using AbstractList. ArrayList and Arrays$ArrayList are both types of AbstractList. That's why you get UnsupportedOperationException: Arrays$ArrayList does not override remove(int) so the method is called on the superclass, AbstractList, which is what is throwing the exception because this method is not implemented on that class (the reason being to allow you to build immutable subclasses).


    評論

    # re: 『Java』java.lang.UnsupportedOperationException at java.util.AbstractLis[未登錄]  回復  更多評論   

    2009-08-15 11:12 by 哈哈
    講解方法很經精確
    喜歡這種講解風格

    # re: 『Java』java.lang.UnsupportedOperationException at java.util.AbstractLis  回復  更多評論   

    2009-09-03 10:02 by javaisgod
    good

    # re: 『Java』java.lang.UnsupportedOperationException at java.util.AbstractLis  回復  更多評論   

    2009-09-09 11:55 by kellyzly
    頂,我的問題解決了,謝謝

    # re: 『Java』java.lang.UnsupportedOperationException at java.util.AbstractLis  回復  更多評論   

    2010-08-18 17:22 by 冬天雞雞好冷
    api 的設計多此一舉還搞個內部類
    主站蜘蛛池模板: 性xxxxx免费视频播放| 三上悠亚在线观看免费| 成人免费黄色网址| 亚洲视频国产视频| 最近中文字幕高清免费中文字幕mv | 青娱乐免费在线视频| 亚洲天堂电影在线观看| 国产高清不卡免费在线| 亚洲乱码一二三四区国产| 无码日韩精品一区二区免费| 亚洲国产乱码最新视频 | 免费国产成人高清视频网站| 亚洲av无码成人影院一区| 蜜臀91精品国产免费观看| 国产亚洲高清在线精品不卡| 免费在线观看黄网| 精品一区二区三区免费观看| 亚洲春色在线视频| 精品久久8x国产免费观看| 亚洲一本到无码av中文字幕| 国产a不卡片精品免费观看| 一级毛片免费毛片毛片| 国产亚洲精品精华液| 91精品免费高清在线| 中文字幕无码亚洲欧洲日韩| 日本特黄a级高清免费大片| 色哟哟国产精品免费观看| 亚洲AV午夜福利精品一区二区| 97av免费视频| 亚洲精品无AMM毛片| 国产精品亚洲综合一区| 一级做a爰全过程免费视频| 在线综合亚洲欧洲综合网站| 免费一级e一片在线播放| 国产免费AV片在线观看| 亚洲中文无码av永久| 亚洲女人被黑人巨大进入| 91福利免费视频| 美女裸免费观看网站| 亚洲av福利无码无一区二区 | 亚洲av成人无码久久精品|