Results 1 to 4 of 4
Thread: Deep Copy Constructor
- 01-29-2013, 05:11 AM #1
Member
- Join Date
- Jan 2013
- Posts
- 6
- Rep Power
- 0
Deep Copy Constructor
I need some help in implementing deep copy constructor and not using the clone method. I understand that in order for the deep copy to work, you are making a copy of the object and not just the address pointing to that object. Otherwise if you make changes to one the original object will also observe the change. However, I am having problem with putting it into practice.
Java Code:/** * This is the main method. * It creates a Team object and then tests the copy constructor. */ public static void main(String[] args) { Team team1 = new Team("The Java Studs", "Bill", "Ted", "Carol", "Alice", new Competition("ACM Programming Contest","The Java Studs", "Team University Java", 2008), new Competition("Antarctic Programming Contest","The Java Studs", "Frigid South", 2010) ); Team team2 = new Team(team1); // Copy constructor // Change competition information for team2 team2.getCompetition1().setYear(2011); team2.getCompetition1().setWinner("Mmm Java"); team2.getCompetition1().setRunnerup("Java By Night Java By Day"); team2.getCompetition2().setYear(2012); // Output information for both team1 and team2. // Team1's should be unchanged from the original values. team1.OutputTeamInfo(); team2.OutputTeamInfo(); }This is my output from command line when I try to run it.Java Code:/** * Team copy constructor; Performs a deep copy from the Competition class * instead of just copying a reference to the same Competition objects. */ public Team(Team otherTeam) { Team copyTeam = new Team(); copyTeam.setCompetition1(this.competition1); copyTeam.setCompetition2(this.competition2); copyTeam.setName1(this.name1); copyTeam.setName2(this.name2); copyTeam.setName3(this.name3); copyTeam.setName4(this.name4); }
Exception in thread "main" java.lang.NullPointerException
at Team.main(Team.java:172)
- 01-29-2013, 06:59 AM #2
Re: Deep Copy Constructor
You have things back-to-front in your copy constructor. You need to copy/set the attributes of the passed-in Team to the current instance ("this")
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-29-2013, 02:48 PM #3
Member
- Join Date
- Jan 2013
- Posts
- 6
- Rep Power
- 0
Re: Deep Copy Constructor
It could be that I need coffee this morning but could you help me understand this with an example? I tried fixing the codes but I feel that I'm only referencing the address because the changes are being observed by both team1 and team2.
- 01-29-2013, 02:58 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Deep Copy Constructor
It's a constructor, not a clone() method, so there is no copyTeam object.
You need to do:
That will give you a second Team object that references the same data that the original Team object references.Java Code:this.setCompetition1(otherTeam.competition1); etc etc
Please do not ask for code as refusal often offends.
Similar Threads
-
deep copy in java
By kmm1977 in forum New To JavaReplies: 1Last Post: 12-09-2011, 05:59 PM -
[SOLVED] Deep copy
By thorne_ in forum New To JavaReplies: 5Last Post: 06-05-2009, 03:51 PM -
Deep copy?!
By deepthought015 in forum New To JavaReplies: 3Last Post: 05-07-2009, 06:31 PM -
Deep Copy Test
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 11:05 PM -
how can i deep copy objects themselves instead of handles or references.. ?
By ishakteyran in forum Advanced JavaReplies: 0Last Post: 12-28-2007, 12:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks