0

I have two classes, say Class Foo , and Class Boo.Foo class contains an arraylist of Boo class. I need to create a deep copy of Foo class, and I'm creating arraylist of Foo itself. How can I accomplish this.

        Class Foo{
        ArrayList<Boo> boovar = new ArrayList<Boo>();
        }

        Class Boo{
        int x;
        void add_x(int y)
        {
            x=y;
        }   
        }

         ArrayList<Foo> foovar = new ArrayList<Foo>();


    ..adding elements to foovar.

now I want to create an ArrayList foovar1 of type Foo, such that it is a deep copy of the objects in foovar. ie, if I modify a boo object in foovar, like deleting x or adding elements, it should not be reflected when i access foovar1.

I know deep copying has been beaten to death, but I really couldn't find articles telling me how to do this. Could someone show me how with a code?

EDIT:

Im trying to create a copy constructor within Foo, like one of the comments mentioned, but I'm really at a loss on how to copy the Boo arraylist to another through the constructor in Foo. Not to mention how I'm going to loop the instances of Foo in the arraylist to create copies of that as well. Any help would be really appreciated!

6
  • 1
    I think you would have to use loops, and copy constructor in Foo class to create a deep copy of your List. Because, you not only need a deep copy of your List, but also deep copy of Foo instances in your List. Commented Nov 22, 2012 at 20:50
  • 1
    did you try searching "java deep copy" here on SO? you will get many posts. some pointing to libraries while others have info and tips. If after that you still have a specific question, you can ask it. check this one it might be relevant to your question: stackoverflow.com/questions/3291830/… Commented Nov 22, 2012 at 20:52
  • Declare your variable as the abstract List<Boo>, not the concrete. It's good practice. Commented Nov 22, 2012 at 20:52
  • Format your code correctly and put that final foovar somewhere it would compile. It is currently outside of a class definition. Commented Nov 22, 2012 at 20:55
  • @ A.J - well, I did. and most people suggested copy contructor/ serialization. I'm not sure how I should structure the copy constructor inside Foo class, since I would need to copy all the elements in Boo. And, also,since Foo itself is an arraylist, I need to copy all instances of Foo in the arraylist as well. Seems complex, and I'm not sure how to do it. Commented Nov 22, 2012 at 21:08

2 Answers 2

1

First, you need to make a copy Constructor in your Boo class:

Class Boo{
  int x;

  //copy constructor 
  public Boo (Boo sourceBoo){
    x = sourceBoo.x;
  }

}

with the above, you can make a copy constructor in Foo class that copies the array of Boos contained in it:

Class Foo{
  ArrayList<Boo> boovar = new ArrayList<Boo>();

  //copy constructor
  public Foo(Foo sourceFoo){
    for (Boo aBoo:sourceFoo.boovar){
      boovar.add(new Boo(aBoo)); 
    }
  }

}

now, you can loop over your array of Foo(s) and copy them one by one into a new array. you can define a method to do that:

public ArrayList<Foo> deepCopyFooList(ArrayList<Foo> sourceArray){
   ArrayList<Foo> targetList = new ArrayList<Foo>();
   for (Foo aFoo:sourceArray){
     targetList.add(new Foo(aFoo));
   }

   return targetList;
}

so to copy your array you would:

ArrayList<Foo> foovar = new ArrayList<Foo>();
ArrayList<Foo> foovarCopy = deepCopyFooList(foovar);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! That not only helped me understand, it also worked without a hitch!! If I could, I would give it more than one upvote!!
0

EDIT:

List<Boo> newCopy = new ArrayList<Boo>(boovar);
newCopy.addAll(boovar);

..provided the elements of boovar are immutable. If not, you should utilize the clone() method on Boo class.

1 Comment

both ways make a shallow copy and not a deep copy as required in the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.