简体   繁体   English

将Java中的addAll函数复制一份

[英]Will addAll function in Java make a copy

When list.addAll(list2) is called will objects in lists be copied to list? list.addAll(list2) ,列表中的对象是否会被复制到列表中? or just copy their references... did not find any explanation on javadoc... 或者只是复制他们的参考文献......没有找到关于javadoc的任何解释......

No copy of the objects or their data are made; 没有制作对象或其数据的副本; their references are simply added to the list object. 他们的引用只是添加到列表对象中。

No, the objects will not be copied; 不,不会复制对象; references to the same objects will be added to the list. 对相同对象的引用将添加到列表中。

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. 对于字符串,由于不可变性,数组副本不会指向原始字符串值,并且您不能指望将指针更改为添加到数组列表中的字符串将导致数组列表中的新值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM