开发者

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

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.


Since you tagged this as BlackBerry and not just J2ME it should be mentioned that there is a CloneableVector class that is part of the BlackBerry APIs. If you are trying to stick to strict J2ME this will be of little use. However, if you are just targeting BlackBerry, then it meets your needs.

net.rim.device.api.util.CloneableVector documentation


You will need to copy the content of your Vector with a loop.

Enumeration e = projects.elements();
while (e.hasMoreElements()) {
    this.projects.addElement((Project) e.nextElement());
}

Need more information see The Java Forum page 2 reply 18, the answer is there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜