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

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

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

    kooyee ‘s blog

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

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

    解決方法是使用Iterator,或者轉(zhuǎn)換為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[未登錄]  回復(fù)  更多評論   

    2009-08-15 11:12 by 哈哈
    講解方法很經(jīng)精確
    喜歡這種講解風(fēng)格

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

    2009-09-03 10:02 by javaisgod
    good

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

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

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

    2010-08-18 17:22 by 冬天雞雞好冷
    api 的設(shè)計多此一舉還搞個內(nèi)部類
    主站蜘蛛池模板: 国产一区二区三区在线免费观看 | a级毛片100部免费观看| 国产免费小视频在线观看| 亚洲国产欧美日韩精品一区二区三区 | 国产乱子伦精品免费视频| 亚洲人6666成人观看| 久久精品毛片免费观看| 2022年亚洲午夜一区二区福利| 日韩在线永久免费播放| 亚洲婷婷综合色高清在线| 免费能直接在线观看黄的视频| 亚洲人成电影在线观看青青| 一个人在线观看视频免费| 亚洲av日韩综合一区二区三区 | 野花香在线视频免费观看大全| 亚洲VA中文字幕无码一二三区| 99re6热视频精品免费观看| 亚洲日韩乱码中文无码蜜桃| 成人人观看的免费毛片| 精品一区二区三区免费毛片| 久久精品国产亚洲精品| 亚洲AV无码专区亚洲AV桃| 国产精品嫩草影院免费| 2020年亚洲天天爽天天噜| 全免费一级午夜毛片| 日韩久久无码免费毛片软件| 亚洲精品成人片在线观看精品字幕 | 亚欧国产一级在线免费| 国产l精品国产亚洲区在线观看| 中文字幕乱码一区二区免费| 亚洲午夜电影一区二区三区| 国产免费私拍一区二区三区| 大地资源在线资源免费观看| 亚洲一区二区三区免费观看| 最近中文字幕无免费| 亚洲午夜无码毛片av久久京东热 | 久久亚洲精品无码VA大香大香| 成人午夜视频免费| 水蜜桃视频在线观看免费播放高清 | 国产精品1024永久免费视频| 白白色免费在线视频|