Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between the **copy** and ** addAll**?

1)Is there any difference between these two keywords for the elements of collections??(Copy those elements to the other collection and addAll those elements to the other collection)

like image 314
Johanna Avatar asked Nov 22 '25 09:11

Johanna


1 Answers

Yes, there is a difference.

From the java docs:

Copy: Copies all of the elements from one list into another. After the operation, the index of each copied element in the destination list will be identical to its index in the source list. The destination list must be at least as long as the source list. If it is longer, the remaining elements in the destination list are unaffected.

Example: Copy [1,2,3] to [4,5,6,7,8] => [1,2,3,7,8]

AddAll: Adds all of the specified elements to the specified collection

Example: AddAll of [1,2,3] to [4,5,6,7,8] => [4,5,6,7,8,1,2,3]

like image 171
Daniel LeCheminant Avatar answered Nov 24 '25 00:11

Daniel LeCheminant