Arrays.asList()的问题记录

1、Arrays.asList()

Arrays.asList()返回的是

复制代码
    public static <T> List<T> asList(T... a) {
        return new ArrayList<>(a);
    }

private static class ArrayList<E> extends AbstractList<E>
        implements RandomAccess, java.io.Serializable

没有实现增删改查方法,使用增删改查会报错

复制代码
Exception in thread "main" java.lang.UnsupportedOperationException

2、new ArrayList<Integer>()

new ArrayList<Integer>()实现了List接口

复制代码
public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable

3、解决方法

可以使用new ArrayList<Integer>(Arrays.asList())返回可增删改查的数组。

List中删除方法重载了。

boolean remove(Object o);

E remove(int index);

当数组中泛型为Integer时,可以采用new Integer()包装的形式,来调用boolean remove(Object o); 方法。

相关推荐
ShineWinsu1 小时前
对于Qt:QGripLayout常见属性、方法以及QSizePolicy的解析
开发语言·c++·qt·面试·gui·客户端·网格
艾莉丝努力练剑1 小时前
【AI大模型接入SDK】C++的SessionManager 线程安全
java·开发语言·前端·c++·人工智能·学习·安全
kyle~1 小时前
FastAPI --- 将业务能力以 API 的形式提供给外部系统
python·fastapi·前后端
aiotto资源分享2 小时前
Python 写 JSON 文件,程序中途退出会留下什么?
java·python·json·jsondecodeerror
利威尔xu2 小时前
Go Mutex 饥饿模式:正常模式等待者饿死,1ms 阈值自动切换崩溃
开发语言·后端·面试·golang·并发编程
梯度下降者3 小时前
Python信号量,条件变量,事件学习
python·条件变量·事件·信号量·线程通信
MSTcheng.3 小时前
【AI应用实战】用 FastAPI + SSE 做一个自己的 AI 聊天网页:流式输出、多轮对话与完整源码
人工智能·python·大模型·fastapi·sse·ai聊天助手
乌暮3 小时前
少写重复 CRUD:用大白话搞懂 MyBatis-Plus
java·开发语言·spring boot·面试·mybatis
不绝1913 小时前
Lua基础语法入门:注释、变量类型与字符串操作
开发语言·lua
the局外人4 小时前
Python 实战检索优化:BM25、向量与重排
数据库·后端·python