0

Hi I have to deep clone an object that contains an array list.

public class Objct1 implements Cloneable {
    ArrayList<Objt> o = new ArrayList();

    public Object1() {
    }

    @Override
    protected Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            // This should never happen
            throw new InternalError(e.toString());
        }
    }
}

public class Objt implements Cloneable {
    private int ca;

    /**
     * @return the x
     */
    public int getCa() {
        return x;
    }

    /**
     * @param x the x to set
     */
    public void setCa(int ca) {
        this.ca = ca;
    }

    @Override
    protected Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            // This should never happen
            throw new InternalError(e.toString());
        }
    }
}

I am trying to clone the Objct1 in this example

Objct1 a = new Objct1 ();

arryOfObjct1[count] = (Objct1) a.clone();
Beau Grantham
  • 3,435
  • 5
  • 33
  • 43
Abdullah Alsharif
  • 543
  • 1
  • 5
  • 7
  • you are not deep cloning, for deep cloning you have to iterate all the array list and Objt should do a deep clone to in the clone method, may be you can serialize them and desiaralize you obtain a deep copy – nachokk Jul 02 '13 at 14:35

1 Answers1

2

Duplicated: How to clone ArrayList and also clone its contents?

The @zerocool answer is not valid because is not cloning objects contained in ArrayList.

Community
  • 1
  • 1
surfealokesea
  • 4,971
  • 4
  • 28
  • 38