java 将Java中的addAll函数复制一份
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7924887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Will addAll function in Java make a copy
提问by Richard
When list.addAll(list2)
is called will objects in lists be copied to list? or just copy their references... did not find any explanation on javadoc...
何时list.addAll(list2)
调用列表中的对象将被复制到列表?或者只是复制他们的参考资料......在javadoc上没有找到任何解释......
回答by Will Chesterfield
No copy of the objects or their data are made; their references are simply added to the list object.
不会复制对象或其数据;它们的引用被简单地添加到列表对象中。
回答by Ernest Friedman-Hill
No, the objects will not be copied; references to the same objects will be added to the list.
不,对象不会被复制;对相同对象的引用将添加到列表中。
回答by jayunit100
In general, java will not copy objects when you "add all", that is, for objects, pointers to the originals are used.
一般情况下,java在“全部添加”时不会复制对象,即对于对象,使用的是指向原始对象的指针。
*But be careful ! For strings, due to immutability, an array copy will not point to the original string values, and you must not expect that changing a pointer to a string that was added to an array list will result in a new value inside the array list.
*但要小心!对于字符串,由于不可变性,数组副本不会指向原始字符串值,并且您不能期望更改指向已添加到数组列表的字符串的指针将导致数组列表中的新值。