Deep copying in java via cloning












0














I currently have two classes, Universe and World. The Universe class has a ArrayList field, that lists all of that Universe's Worlds. I want to be able to copy a Universe and then add a world to it, so that I have two Universe objects, one with one World less than the other.



This is the Universe class:



public class Universe {
private ArrayList<World> worlds;
private int worldCount;
private boolean reflex;
private boolean trans;
private boolean symm;
private boolean hereditary;

public Universe(ArrayList<World> worlds, int worldCount, boolean reflex, boolean trans, boolean symm, boolean hereditary) {
this.worlds = worlds;
this.worldCount = worldCount;
this.trans = trans;
this.reflex = reflex;
this.symm = symm;
this.hereditary = hereditary;
if (this.symm && this.trans) { // symmetry and transitivty makes reflexivity
this.reflex = true;
}
}
public Universe(Universe u) { // creates a shallow copy of the other universe
this(u.getWorlds(), u.getWorldCount(), u.getReflex(), u.getTrans(), u.getSymm(), u.getHereditary());
}
@Override
public Object clone() {
Universe u = null;
try {
u = (Universe) super.clone();
}catch(CloneNotSupportedException e) {
u = new Universe(this.getWorlds(), this.getWorldCount(), this.getReflex(), this.getTrans(), this.getSymm(), this.getHereditary());
}
return u;
}
}


And the World class:



public class World {
private Universe parentUniverse;
private String worldName;
private ArrayList<Relation> relations;
private ArrayList<ExprStr> expressions;


public World(Universe u) {
this.parentUniverse = u;
int count = u.getWorldCount();
String countStr = Integer.toString(count);
this.worldName = "";
this.worldName += 'w' + countStr;
this.relations = new ArrayList<Relation>();
if (this.parentUniverse.getReflex()) {
this.addRelation(this, true, true);
}
this.expressions = new ArrayList<ExprStr>();
}
}


The World class names itself within its own universe, and the toString method returns that name. The toString method of Universe returns the list of all Worlds.



I have the code:



Universe y = new Universe();
World d = new World(y);
y.addWorld(d);

Universe x = (Universe) y.clone(); // have to type cast to use clone()
World d1 = new World(x);
x.addWorld(d1);

System.out.println(y);
System.out.println(x);


But the output is:



[w0, w1]
[w0, w1]


Even though, if deep copied properly, I expected that one Universe would have more worlds than the other.



Do I need to deep copy the World class too? What am I doing wrong?



Thanks heaps! :)










share|improve this question


















  • 1




    Yes, you need to deep copy anything you want to be a distinct reference.
    – shmosel
    Nov 21 at 1:14






  • 1




    You need to deep copy the ArrayList itself, e.g. using new ArrayList<>(u.getWorlds()) in the copy-constructor.
    – Andreas
    Nov 21 at 1:15










  • Why do you even try super.clone() when you haven't implemented Cloneable? It'll fail, so why not just do the copy-constructor directly?
    – Andreas
    Nov 21 at 1:17










  • Youre right, I forgot to implement cloneable. Also, I didnt realise it was possible to copy an ArrayList like that, and it works without cloning, so thanks for the much simpler solution! :)
    – Michael Tracey
    Nov 21 at 2:08
















0














I currently have two classes, Universe and World. The Universe class has a ArrayList field, that lists all of that Universe's Worlds. I want to be able to copy a Universe and then add a world to it, so that I have two Universe objects, one with one World less than the other.



This is the Universe class:



public class Universe {
private ArrayList<World> worlds;
private int worldCount;
private boolean reflex;
private boolean trans;
private boolean symm;
private boolean hereditary;

public Universe(ArrayList<World> worlds, int worldCount, boolean reflex, boolean trans, boolean symm, boolean hereditary) {
this.worlds = worlds;
this.worldCount = worldCount;
this.trans = trans;
this.reflex = reflex;
this.symm = symm;
this.hereditary = hereditary;
if (this.symm && this.trans) { // symmetry and transitivty makes reflexivity
this.reflex = true;
}
}
public Universe(Universe u) { // creates a shallow copy of the other universe
this(u.getWorlds(), u.getWorldCount(), u.getReflex(), u.getTrans(), u.getSymm(), u.getHereditary());
}
@Override
public Object clone() {
Universe u = null;
try {
u = (Universe) super.clone();
}catch(CloneNotSupportedException e) {
u = new Universe(this.getWorlds(), this.getWorldCount(), this.getReflex(), this.getTrans(), this.getSymm(), this.getHereditary());
}
return u;
}
}


And the World class:



public class World {
private Universe parentUniverse;
private String worldName;
private ArrayList<Relation> relations;
private ArrayList<ExprStr> expressions;


public World(Universe u) {
this.parentUniverse = u;
int count = u.getWorldCount();
String countStr = Integer.toString(count);
this.worldName = "";
this.worldName += 'w' + countStr;
this.relations = new ArrayList<Relation>();
if (this.parentUniverse.getReflex()) {
this.addRelation(this, true, true);
}
this.expressions = new ArrayList<ExprStr>();
}
}


The World class names itself within its own universe, and the toString method returns that name. The toString method of Universe returns the list of all Worlds.



I have the code:



Universe y = new Universe();
World d = new World(y);
y.addWorld(d);

Universe x = (Universe) y.clone(); // have to type cast to use clone()
World d1 = new World(x);
x.addWorld(d1);

System.out.println(y);
System.out.println(x);


But the output is:



[w0, w1]
[w0, w1]


Even though, if deep copied properly, I expected that one Universe would have more worlds than the other.



Do I need to deep copy the World class too? What am I doing wrong?



Thanks heaps! :)










share|improve this question


















  • 1




    Yes, you need to deep copy anything you want to be a distinct reference.
    – shmosel
    Nov 21 at 1:14






  • 1




    You need to deep copy the ArrayList itself, e.g. using new ArrayList<>(u.getWorlds()) in the copy-constructor.
    – Andreas
    Nov 21 at 1:15










  • Why do you even try super.clone() when you haven't implemented Cloneable? It'll fail, so why not just do the copy-constructor directly?
    – Andreas
    Nov 21 at 1:17










  • Youre right, I forgot to implement cloneable. Also, I didnt realise it was possible to copy an ArrayList like that, and it works without cloning, so thanks for the much simpler solution! :)
    – Michael Tracey
    Nov 21 at 2:08














0












0








0







I currently have two classes, Universe and World. The Universe class has a ArrayList field, that lists all of that Universe's Worlds. I want to be able to copy a Universe and then add a world to it, so that I have two Universe objects, one with one World less than the other.



This is the Universe class:



public class Universe {
private ArrayList<World> worlds;
private int worldCount;
private boolean reflex;
private boolean trans;
private boolean symm;
private boolean hereditary;

public Universe(ArrayList<World> worlds, int worldCount, boolean reflex, boolean trans, boolean symm, boolean hereditary) {
this.worlds = worlds;
this.worldCount = worldCount;
this.trans = trans;
this.reflex = reflex;
this.symm = symm;
this.hereditary = hereditary;
if (this.symm && this.trans) { // symmetry and transitivty makes reflexivity
this.reflex = true;
}
}
public Universe(Universe u) { // creates a shallow copy of the other universe
this(u.getWorlds(), u.getWorldCount(), u.getReflex(), u.getTrans(), u.getSymm(), u.getHereditary());
}
@Override
public Object clone() {
Universe u = null;
try {
u = (Universe) super.clone();
}catch(CloneNotSupportedException e) {
u = new Universe(this.getWorlds(), this.getWorldCount(), this.getReflex(), this.getTrans(), this.getSymm(), this.getHereditary());
}
return u;
}
}


And the World class:



public class World {
private Universe parentUniverse;
private String worldName;
private ArrayList<Relation> relations;
private ArrayList<ExprStr> expressions;


public World(Universe u) {
this.parentUniverse = u;
int count = u.getWorldCount();
String countStr = Integer.toString(count);
this.worldName = "";
this.worldName += 'w' + countStr;
this.relations = new ArrayList<Relation>();
if (this.parentUniverse.getReflex()) {
this.addRelation(this, true, true);
}
this.expressions = new ArrayList<ExprStr>();
}
}


The World class names itself within its own universe, and the toString method returns that name. The toString method of Universe returns the list of all Worlds.



I have the code:



Universe y = new Universe();
World d = new World(y);
y.addWorld(d);

Universe x = (Universe) y.clone(); // have to type cast to use clone()
World d1 = new World(x);
x.addWorld(d1);

System.out.println(y);
System.out.println(x);


But the output is:



[w0, w1]
[w0, w1]


Even though, if deep copied properly, I expected that one Universe would have more worlds than the other.



Do I need to deep copy the World class too? What am I doing wrong?



Thanks heaps! :)










share|improve this question













I currently have two classes, Universe and World. The Universe class has a ArrayList field, that lists all of that Universe's Worlds. I want to be able to copy a Universe and then add a world to it, so that I have two Universe objects, one with one World less than the other.



This is the Universe class:



public class Universe {
private ArrayList<World> worlds;
private int worldCount;
private boolean reflex;
private boolean trans;
private boolean symm;
private boolean hereditary;

public Universe(ArrayList<World> worlds, int worldCount, boolean reflex, boolean trans, boolean symm, boolean hereditary) {
this.worlds = worlds;
this.worldCount = worldCount;
this.trans = trans;
this.reflex = reflex;
this.symm = symm;
this.hereditary = hereditary;
if (this.symm && this.trans) { // symmetry and transitivty makes reflexivity
this.reflex = true;
}
}
public Universe(Universe u) { // creates a shallow copy of the other universe
this(u.getWorlds(), u.getWorldCount(), u.getReflex(), u.getTrans(), u.getSymm(), u.getHereditary());
}
@Override
public Object clone() {
Universe u = null;
try {
u = (Universe) super.clone();
}catch(CloneNotSupportedException e) {
u = new Universe(this.getWorlds(), this.getWorldCount(), this.getReflex(), this.getTrans(), this.getSymm(), this.getHereditary());
}
return u;
}
}


And the World class:



public class World {
private Universe parentUniverse;
private String worldName;
private ArrayList<Relation> relations;
private ArrayList<ExprStr> expressions;


public World(Universe u) {
this.parentUniverse = u;
int count = u.getWorldCount();
String countStr = Integer.toString(count);
this.worldName = "";
this.worldName += 'w' + countStr;
this.relations = new ArrayList<Relation>();
if (this.parentUniverse.getReflex()) {
this.addRelation(this, true, true);
}
this.expressions = new ArrayList<ExprStr>();
}
}


The World class names itself within its own universe, and the toString method returns that name. The toString method of Universe returns the list of all Worlds.



I have the code:



Universe y = new Universe();
World d = new World(y);
y.addWorld(d);

Universe x = (Universe) y.clone(); // have to type cast to use clone()
World d1 = new World(x);
x.addWorld(d1);

System.out.println(y);
System.out.println(x);


But the output is:



[w0, w1]
[w0, w1]


Even though, if deep copied properly, I expected that one Universe would have more worlds than the other.



Do I need to deep copy the World class too? What am I doing wrong?



Thanks heaps! :)







java reference deep-copy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 1:04









Michael Tracey

276




276








  • 1




    Yes, you need to deep copy anything you want to be a distinct reference.
    – shmosel
    Nov 21 at 1:14






  • 1




    You need to deep copy the ArrayList itself, e.g. using new ArrayList<>(u.getWorlds()) in the copy-constructor.
    – Andreas
    Nov 21 at 1:15










  • Why do you even try super.clone() when you haven't implemented Cloneable? It'll fail, so why not just do the copy-constructor directly?
    – Andreas
    Nov 21 at 1:17










  • Youre right, I forgot to implement cloneable. Also, I didnt realise it was possible to copy an ArrayList like that, and it works without cloning, so thanks for the much simpler solution! :)
    – Michael Tracey
    Nov 21 at 2:08














  • 1




    Yes, you need to deep copy anything you want to be a distinct reference.
    – shmosel
    Nov 21 at 1:14






  • 1




    You need to deep copy the ArrayList itself, e.g. using new ArrayList<>(u.getWorlds()) in the copy-constructor.
    – Andreas
    Nov 21 at 1:15










  • Why do you even try super.clone() when you haven't implemented Cloneable? It'll fail, so why not just do the copy-constructor directly?
    – Andreas
    Nov 21 at 1:17










  • Youre right, I forgot to implement cloneable. Also, I didnt realise it was possible to copy an ArrayList like that, and it works without cloning, so thanks for the much simpler solution! :)
    – Michael Tracey
    Nov 21 at 2:08








1




1




Yes, you need to deep copy anything you want to be a distinct reference.
– shmosel
Nov 21 at 1:14




Yes, you need to deep copy anything you want to be a distinct reference.
– shmosel
Nov 21 at 1:14




1




1




You need to deep copy the ArrayList itself, e.g. using new ArrayList<>(u.getWorlds()) in the copy-constructor.
– Andreas
Nov 21 at 1:15




You need to deep copy the ArrayList itself, e.g. using new ArrayList<>(u.getWorlds()) in the copy-constructor.
– Andreas
Nov 21 at 1:15












Why do you even try super.clone() when you haven't implemented Cloneable? It'll fail, so why not just do the copy-constructor directly?
– Andreas
Nov 21 at 1:17




Why do you even try super.clone() when you haven't implemented Cloneable? It'll fail, so why not just do the copy-constructor directly?
– Andreas
Nov 21 at 1:17












Youre right, I forgot to implement cloneable. Also, I didnt realise it was possible to copy an ArrayList like that, and it works without cloning, so thanks for the much simpler solution! :)
– Michael Tracey
Nov 21 at 2:08




Youre right, I forgot to implement cloneable. Also, I didnt realise it was possible to copy an ArrayList like that, and it works without cloning, so thanks for the much simpler solution! :)
– Michael Tracey
Nov 21 at 2:08

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403889%2fdeep-copying-in-java-via-cloning%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403889%2fdeep-copying-in-java-via-cloning%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python