How would I deep copy a vector in J2ME / BlackBerry?
Unfortunately, there is no reliable way to do a deep copy on a Vector of objects.
Just a quick review of what I believe a "deep copy" is: A deep copy is a copy where not only are the contents of a collection (vector, in this case) copied, but the objects contained in the Vector are copied independently. In other words: If vector V contains A, and a copy (V') of V is made, a copy of A (A') in V' is unaffected by any changes to A and vice versa.
Typically, this would be implemented by "cloning" an object. Unfortunately, if you have no control over the objects in the Vector, you have no reasonable way of cloning them, especially since JavaME does not possess a Cloneable interface (as far as I could find).
Of course, if you do control the objects, you might make your own Cloneable interface that specifies a clone() method that returns a completely independent copy of the object. Then, you must ensure that your special cloning Vector only accepts objects that implement that interface. From there, it's pretty easy (code-wise) for you to make a Vector that can clone itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With