Will addAll function in Java make a copy

主宰稳场 提交于 2019-12-03 16:05:15

问题


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...


回答1:


No copy of the objects or their data are made; their references are simply added to the list object.




回答2:


No, the objects will not be copied; references to the same objects will be added to the list.




回答3:


In general, java will not copy objects when you "add all", that is, for objects, pointers to the originals are used.

* 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.



来源:https://stackoverflow.com/questions/7924887/will-addall-function-in-java-make-a-copy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!