Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

toArray().. shallow or deep copies

520 views
Skip to first unread message

Jason S.

unread,
Dec 8, 2014, 1:02:03 AM12/8/14
to
Question... when using the toArray() method on a Collection, are the
references in the array a shallow or deep copy of the objects in the
Collection?

Thanks

Amit Sharma

unread,
Dec 8, 2014, 5:10:15 AM12/8/14
to
Some operations on array are more efficient than that of collection. Like a collecton provides a specified order of elements the using toArray() we can convert it in array and use various operations like reversing the array. Instead of all these things searching and sorting are more efficient in case of array. And mostly we use toArray() method in case of junit framework to test our method. We can't compare two lists in junit efficiently so first convert them in form of arrays and use arrayEquals(array1, array2) method.

Eric Sosman

unread,
Dec 8, 2014, 8:04:53 AM12/8/14
to
"Shallow" and "deep" aren't rigorously defined, so I'll try to
answer without using those terms ...

The Collection contains reference values that (if non-null) point
to objects. The array generated by toArray() contains copies of those
reference values, pointing to the same objects. Once created, the
Collection and the array are independent in the sense that modifying
one has no effect on the other. If the pointed-to objects are mutable,
modifications made via the array's references are visible via the
Collection's references, and vice versa.

--
eso...@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

Arne Vajhøj

unread,
Dec 8, 2014, 8:58:02 PM12/8/14
to
On 12/8/2014 1:01 AM, Jason S. wrote:
Shallow.

Arne


Jason Savlov

unread,
Dec 9, 2014, 12:36:39 AM12/9/14
to
Thanks for the help, guys. Answers helped me understand the assignment
I was working on.

Cheers!

0 new messages