gpt4 book ai didi

java - java中不可修改的列表

转载 作者:行者123 更新时间:2023-12-01 17:17:09 26 4
gpt4 key购买 nike

如果我尝试修改java中设置为不可修改的列表,我对实际会发生什么感到困惑,我对Java相对较新,从我可以找到的其他人问过类似的问题来看它仍然可以修改吗?如果是的话,这是为什么?

谢谢。

最佳答案

直接来自JavaDoc :

Returns an unmodifiable view of the specified list. This method allows modules to provide users with "read-only" access to internal lists. Query operations on the returned list "read through" to the specified list, and attempts to modify the returned list, whether direct or via its iterator, result in an UnsupportedOperationException.

因此它将抛出UnsupportedOperationException

或者从源代码:

    public E set(int index, E element) {
throw new UnsupportedOperationException();
}
public void add(int index, E element) {
throw new UnsupportedOperationException();
}
public E remove(int index) {
throw new UnsupportedOperationException();
}
//etc...

关于java - java中不可修改的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21218364/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com