<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 的設計多此一舉還搞個內部類
    主站蜘蛛池模板: 亚洲精品mv在线观看| 精品剧情v国产在免费线观看| www成人免费观看网站| 特黄特色的大片观看免费视频| 香蕉视频免费在线播放| 一级毛片大全免费播放下载| kk4kk免费视频毛片| free哆拍拍免费永久视频| 两个人日本免费完整版在线观看1| 一级日本高清视频免费观看| 中文字幕免费人成乱码中国| 拍拍拍无挡免费视频网站| 久久久久久免费一区二区三区| 人妻丰满熟妇无码区免费| 亚洲毛片免费观看| 毛片高清视频在线看免费观看| 色吊丝最新永久免费观看网站| 国产午夜免费福利红片| 亚洲国产精品成人| 久久亚洲国产午夜精品理论片| 亚洲电影中文字幕| 亚洲三级在线免费观看| 亚洲人成色777777精品| 国产成人综合亚洲绿色| 国产精品成人啪精品视频免费| 久久99精品免费视频| 久久久久久精品成人免费图片| 四虎影视免费在线| 中文字幕亚洲图片| 久久精品亚洲中文字幕无码麻豆| 亚洲人成免费网站| 人人爽人人爽人人片A免费| a级毛片在线免费| 四虎在线成人免费网站| 国产片免费在线观看| 亚洲色无码一区二区三区| 精品无码一区二区三区亚洲桃色| 亚洲欧美一区二区三区日产| 一级做a爰片久久免费| 免费A级毛片av无码| 午夜毛片不卡免费观看视频|