这是从List.addAll文档中摘录的一个(典型的?)示例,它似乎并未说明目标列表中的添加内容是否为深层副本。除了测试,快速解析文档的一般方法或可以确定标准Java类和方法是否深复制的一般理解是什么? Oracle writeup on List.addAll

addAll

boolean addAll(Collection<? extends E> c)

    Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)

    Specified by:
        addAll in interface Collection<E>

    Parameters:
        c - collection containing elements to be added to this list
    Returns:
        true if this list changed as a result of the call
    Throws:
        UnsupportedOperationException - if the addAll operation is not supported by this list
        ClassCastException - if the class of an element of the specified collection prevents it from being added to this list
        NullPointerException - if the specified collection contains one or more null elements and this list does not permit null elements, or if the specified collection is null
        IllegalArgumentException - if some property of an element of the specified collection prevents it from being added to this list
    See Also:
        add(Object)

最佳答案

除非有说明指定需要深拷贝,否则我总是以为不会。进行深拷贝非常罕见,特别是因为它依赖于支持克隆的对象本身。

关于java - 在不测试Java方法是否进行深拷贝的情况下确定的一般规则是什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6881995/

10-12 22:49