Will addAll function in Java make a copy

后端 未结 3 778
我寻月下人不归
我寻月下人不归 2021-02-07 00:16

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

相关标签:
3条回答
  • 2021-02-07 00:29

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

    0 讨论(0)
  • 2021-02-07 00:32

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

    0 讨论(0)
  • 2021-02-07 00:39

    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.

    0 讨论(0)
提交回复
热议问题