Results 1 to 6 of 6
  1. #1
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Deep copy arraylist (NO CLONE)

    Hi. I have a public class called MyPersonList and I was wondering how to create a deep copy of arraylist for the copy constructor. The elements are mutable, so i Know i can not use shallow copy. Im new to this topic and help would be awesome.

    Here's what I have

    Java Code:
     
    
    public class MyPersonList {
    
    	
    	public ArrayList<Person> people;
    
    	/**
    	 * Instantiates the "people" variable as a new (empty) ArrayList of
    	 * Person objects.
    	 */
    	public MyPersonList(){
    		
    		people = new ArrayList<Person>();
    	}
    	
    	
    	/**
    	 * A copy constructor which makes the right kind of copy considering
    	 * a Person is mutable.
    	 */
    	public MyPersonList(MyPersonList other) {
    
             //lost 
             // dont know how to begin
             // new to syntax, I dont kknow the syntax

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Deep copy arraylist (NO CLONE)

    just use a for each loop and create a new arrayList, then inside the for each loop

    obviously if you want to have a field for the arraylist you can adjust this to suit you.

    Java Code:
    ArrayList<Person> newList = new ArrayList<Person>();
    for(Person p : oldList) {
        newList.add(p.clone()); // new List is the new ArrayList you created
    }

  3. #3
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: Deep copy arraylist (NO CLONE)

    For this class, I can't use clone.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,420
    Rep Power
    5

    Default Re: Deep copy arraylist (NO CLONE)

    It depends on the nature of the Person class. One way is to simply call a constructor using the get methods for the instance variables as the paarmeters.

    e.g

    Java Code:
    MyClass a = new MyClass(arg0, arg1, arg2);
    // some time later, args may have changed.
    MyClass CopyOfa = new MyClass(a.getArg0(), a.getArg1(), a.getArg2());

    For more complex classes that have private fields you could write a method that provides a copy of the current instance, which includes all public and private fields. In essence, it is a clone but you are not using the clone method. If the Person class has instance fields such as HashMaps then those need to be iterated over and duplicated. Deep copies can get complex and can involve reflection and recursive programming.

    Regards,
    Jim
    Last edited by jim829; 05-02-2013 at 02:22 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default Re: Deep copy arraylist (NO CLONE)

    Or provide a constructor for Person that takes Person as an object.
    Or provide a deepCopy() method that returns a deep copy.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    79
    Rep Power
    0

    Default Re: Deep copy arraylist (NO CLONE)

    What I would do is :
    ArrayList<Person> newList = new ArrayList<Person>();
    for(Person p : oldList) {
    newList.add(p.getNewCopy());
    }

    In person I would add the method

    public Person getNewCopy(){
    Person p= new Person();
    p.setA1(a1);
    p.setA2(a2);
    etc...
    return p;
    }

    A1, A2, etc is the attributes in Person. a1 and a2 are the current object in the current class Person.

    By doing this you get a new object of Person for each existing Person to "Copy" them.

Similar Threads

  1. Deep Copy Constructor
    By tngo in forum New To Java
    Replies: 3
    Last Post: 01-29-2013, 02:58 PM
  2. deep copy in java
    By kmm1977 in forum New To Java
    Replies: 1
    Last Post: 12-09-2011, 05:59 PM
  3. [SOLVED] Deep copy
    By thorne_ in forum New To Java
    Replies: 5
    Last Post: 06-05-2009, 03:51 PM
  4. Deep copy?!
    By deepthought015 in forum New To Java
    Replies: 3
    Last Post: 05-07-2009, 06:31 PM
  5. Deep Copy Test
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:05 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •