日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

ArrayList Iterator remove java.lang.UnsupportedOperationException

發布時間:2023/11/27 生活经验 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ArrayList Iterator remove java.lang.UnsupportedOperationException 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?在使用Arrays.asList()后調用add,remove這些method時出現 java.lang.UnsupportedOperationException異常。這是由于Arrays.asList() 返回java.util.Arrays$ArrayList, 而不是ArrayList。Arrays$ArrayList和ArrayList都是繼承AbstractList,remove,add等 method在AbstractList中是默認throw UnsupportedOperationException而且不作任何操作。ArrayList override這些method來對list進行操作,但是Arrays$ArrayList沒有override remove(),add()等,所以throw UnsupportedOperationException。

????? 例子:

package com.test;

????import java.util.Arrays;
import java.util.List;

public class TestUnsupported {
? public static void main(String[] args) {
??????? String[] s = {
??????????? "one", "two", "three", "four", "five",
??????????? "six", "seven", "eight", "nine", "ten",
????????? };

??????? List a = Arrays.asList(s);
??????? System.out.println(
????????? "a.contains(" + s[0] + ") = " +
????????? a.contains(s[0]));
??????? a.add("eleven"); // Unsupported
??????? a.remove(s[0]); // Unsupported
????? }
}

運行后,拋出異常如下:

Exception in thread "main" java.lang.UnsupportedOperationException
?at java.util.AbstractList.add(AbstractList.java:151)
?at java.util.AbstractList.add(AbstractList.java:89)
?at com.test.TestUnsupported.main(TestUnsupported.java:28)

?

解決方法是轉換為ArrayList

List?arrayList?=?new?ArrayList(a);

或者直接這么寫? List?arrayList?=?new?ArrayList(Arrays.asList(s));

參考

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).

轉載于:https://www.cnblogs.com/toSeeMyDream/p/5253794.html

總結

以上是生活随笔為你收集整理的ArrayList Iterator remove java.lang.UnsupportedOperationException的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。