Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I deep copy a vector in J2ME / BlackBerry?

How would I deep copy a vector in J2ME / BlackBerry?

like image 485
Dan Gifford Avatar asked Mar 22 '10 17:03

Dan Gifford


1 Answers

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.

like image 166
Eric Avatar answered Nov 12 '22 15:11

Eric