Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

copies of objects

 
Michael Lipton
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello, i'm stumped and hoping that someone can help me out...
i've created a class called IntState, which stores the state of a system
a second class Optimization performs an optimization algorithm on an original system state to find the state of optimum reliabilty... however, i'm having a major problem...

when executing the following (psuedo)code, the method makes changes to the currentState object as well as the state object, even though currentState is never modified, and is used only to supply a copy.

public IntState getNextState( IntState currentState )
IntState state = currentState.copy();
modify state
return state


anyone have any ideas??? i'm getting really desperate and would really appreciate it...
thanks in advance.
mike
 
Yevgeniy Treyvus
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your copy() have the same functionality of a clone() method? If so why not use a clone() method instead? Are you using deep or shallow copying? I think you have to be cafeful if you're deep-copying.
[ April 07, 2005: Message edited by: Yevgeniy Treyvus ]
 
Michael Lipton
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i've made a little progress, i think the problem is that i'm making a shallow copy, but i need to be making a deep copy... do you know of any simple ways to do this??
 
Hung Yee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sun has a tech tip article on making 'deep copies' that might interest you:
http://java.sun.com/developer/JDCTechTips/2001/tt0410.html#making
 
Ed Wallen
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael,

Here is a method that I wrote in one of my framework utilities. This is a simple way of making a deep copy. If performance is critical, there are faster ways of doing this.........but this should get you past your problem.



Hope this helps.

-Ed
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can use it, Jakarta Commons Lang has a SerializationUtils.clone() method that would be of use to you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic