Java 数组转换成List,然后执行add或remove抛异常UnsupportedOperationException问题的解决

文章转自:http://blog.csdn.net/fanpei_moukoy/article/details/52504363

在使用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 CloudPanSys_Test;  

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

public class Spilt {  

    public static void main(String[] args) {  
        String path = "/root/m/123.txt";  
        path = getClientPath(path);  

    }  

    public static String getClientPath(String svrPath) {  

        StringBuilder newpath = new StringBuilder();  
        String[] svrPaths = svrPath.split("/");  
        List<String> list = Arrays.asList(svrPaths);  

        list.remove(0);//执行抛出异常  
        for (String string : list) {  
            newpath.append("/" + string);  
        }  
        return newpath.toString();  
    }  

}  

运行后,抛出异常如下:

解决:使用Iterator,或者转换为ArrayList

List list = Arrays.asList(svrPaths);  
List<String> list2 = new ArrayList<String>(list);  
list2.remove(0); 

参考:
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).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`List.removeIf`方法在Java中用于删除满足特定条件的元素。这个方法在Java 8中被引入,用于简化集合的操作。然而,这个方法在某些情况下可能会出`UnsupportedOperationException: null`异常。 这个异常通常发生在以下情况: 1. 当列表为空时,调用`removeIf`方法会异常。因为在这个情况下,没有任何元素可以被删除。 2. 当列表中存在空对象(null)时,调用`removeIf`方法也会异常。因为当尝试删除一个空对象时,会引发NullPointerException。 为了避免这个问题,你可以在调用`removeIf`方法之前检查列表是否为空,或者确保列表中的所有元素都不是null。 以下是一个简单的例子: ```java List<String> list = new ArrayList<>(); // 添加一些元素到列表中 list.add("Element 1"); list.add("Element 2"); list.add(null); // 注意这里添加了一个空对象 // 检查列表是否为空,然后再调用 removeIf 方法 if (!list.isEmpty()) { list.removeIf(element -> element.equals("Element 1")); // 删除 "Element 1" } ``` 这个例子中的代码在删除元素之前首先检查了列表是否为空,并确保没有null元素存在。如果确实需要删除某个特定的元素,你只需检查并决定是否包含这个条件即可。这样就不会再异常了。 另外,对于你遇到的具体问题,你可能需要检查你的代码中是否存在上述问题,或者是否在调用`removeIf`方法之前对列表进行了其他操作导致其变为空或包含null元素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值