user1567060
user1567060

Reputation: 179

Java - Deep clone a Vector that has elements of multiple types

I have some sample code below from a project I am working on. In it, I need to deep clone the elements of Vector v into Vector w. In other words, every individual element needs to reference its own place in memory.

In my program, I need to make objects of different types and add them to a Vector. I then need to clone each element in that vector (meaning give them their own place in memory) and add those cloned elements to a new Vector. The problem is I don't know how to clone when the object type is unknown to the programmer. If every element were of the same type, I could just cast to that type. But that is not the case here.

In my code, I make sure the given element implements Cloneable, but I don't know how to do the actual cloning. (Be it somehow casting to the proper type and then calling clone() or some other way.)

Thanks. My code is below and any help is greatly appreciated.

import java.util.*;

public class Test {
    public static void main(String[] arguments) {
        // Establish variables
        Point p1 = new Point();
        Point p2 = new Point(1, 2);
        A a1 = new A();
        A a2 = new A(1);

        // Create vector v and add variables to vector
        Vector v = new Vector();
        v.add(p1);
        v.add(p2);
        v.add(a1);
        v.add(a2);

        // Change p1.x
        p1.x = 1;

        // Create vector w and assign elements from v into vector
        Vector w = new Vector();
        int n = v.size();
        for (int i = 0; i < n; i++)
            if (v.get(i) instanceof Cloneable)
                w.add(v.get(i));

        // print elements in vectors
        System.out.println(v);
        System.out.println(w); //elements seem to reference the same area in memory
    }
}

class Point implements Cloneable {
    int x;
    int y;

    Point() {
        x = 0;
        y = 0;
    }

    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public Object Clone() {
        try {return super.clone();}
        catch (CloneNotSupportedException exception) {return null;}
    }

    public String toString() {return "Point[x=" + x + ",y=" + y + "]";}
}

class A implements Cloneable {
    int i;

    A() {i = 0;}

    A(int i) {this.i = i;}

    public Object Clone() {
        try {return super.clone();}
        catch (CloneNotSupportedException exception) {return null;}
    }

    public String toString() {return "A[i=" + i + "]";}
}

Upvotes: 0

Views: 820

Answers (1)

prasanth
prasanth

Reputation: 3602

You can do deep serialization of the entire vector instead of serializing individual elements.

  public static <T extends Serializable> T deepCopyOfSerializable(T o) throws Exception
{
    if (o == null)
        return null;

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bos);

    oos.writeObject(o);
    bos.close();
    oos.close();

    ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(bis);

    T t = (T) ois.readObject();
    bis.close();
    ois.close();
    return t;
}

Upvotes: 1

Related Questions