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!

Floose
  • 525
  • 2
  • 7
  • 15
  • 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. – Rohit Jain Nov 22 '12 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: http://stackoverflow.com/questions/3291830/deep-copy-and-arraylist-java –  Nov 22 '12 at 20:52
  • Declare your variable as the abstract `List`, not the concrete. It's good practice. – Bohemian Nov 22 '12 at 20:52
  • Format your code correctly and put that final `foovar` somewhere it would compile. It is currently outside of a class definition. – Cory Kendall Nov 22 '12 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. – Floose Nov 22 '12 at 21:08
  • @CoryKendall I was just providing an example class to explain the problem.. It wasn't code.. just an example that you could use to explain your solution – Floose Nov 22 '12 at 21:10

2 Answers2

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);
  • 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!! – Floose Nov 23 '12 at 07:14
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.

Jiri Kremser
  • 12,471
  • 7
  • 45
  • 72
  • both ways make a shallow copy and not a deep copy as required in the question. –  Nov 22 '12 at 22:16