Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-28-2007, 01:28 PM
Member
 
Join Date: Dec 2007
Posts: 3
ishakteyran is on a distinguished road
how can objects themselves be copied???
Code:
Code:
/* * Node.java * * Created on December 26, 2007, 4:23 AM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ import java.util.*; /** * * @author isaac */ public class Node<V> implements Cloneable{ NodeItem item; Node parent; HashMap<String,Node> map; public String str; /** Creates a new instance of Node */ public Node() { this.map=new HashMap<String,Node>(); this.item=null; Set<String> s=map.keySet(); Iterator<String> i=s.iterator(); } public Node(Node nod){ this.item=nod.item; this.map=new HashMap<String,Node>(); this.map= nod.map; this.parent=nod.parent; this.str=nod.str; } public Node getParent(){ return this.parent; } public void setParent(Node nd) { this.parent=nd; } public void setItem(NodeItem a){this.item=a;} public void dene() throws CloneNotSupportedException{ Node<String> n1=new Node<String>(); n1.str="1"; NodeItem<Integer> it1=new NodeItem<Integer>(2,"2",n1); n1.item=it1; Node<String> n2=new Node<String>(); n2.str="2"; n2.item=it1; n2.parent=n1; Node<String> n3=new Node<String>(); n3.str="3"; n3.item=it1; n3.parent=n2; Node<String> n7=new Node<String>(); n7.str="7"; n7.item=it1; n7.parent=n3; n3.map.put("key",n7); n2.map.put("key",n3); n1.map.put("key",n2); Node<String> n4=new Node<String>(n3); NodeItem<Integer> it6=new NodeItem<Integer>(1,"1",n4); n4.str="4"; n4.item=it6; Node<String> n6 =new Node<String>(); n6.str="6"; n6.item=it6; n4.parent=n6; n4.map.get("key").parent=n4; n4.map.get("key").item=it6; n6.map.put("key2",n4); } private class NodeItem<V> { String key; V info; Node x; public NodeItem(V v, String k,Node x1){ this.info=v; this.key=k; this.x=x1; } } }
HI TO ALL...

aS YOU CAN SEE FROM THE CODE OF DENE() METHOD OF THIS CLASS I AM TRYING TO GET A DEEP COPY OF VARIABLE N3.. THAT IS WHEN I COPY THE CONTENTS OF N3 TO N4 IN THE DENE() METHOD I ALSO WANT THAT NOT THE HANDLES BUT ALL THE OBJECT CONTENTS OF N3 TO BE COPIED TO N4.. WHEN THIS DONE I WANT THAT WHEN I EXECUTE THE n4.map.get("key").parent=n4; LINE AND THEN ASK FOR WHAT THE VALUE OF n3.map.get("key").parent be n2 ...

in other words let me explain my problem thus...

i try to build a directory hierarchy system.. i want to include the recurs,ive copy of directories...

as it can be seen in the code of dene() method i first create a hierarchy -from top to down - n1->n2-> n3-> n7 ... and then i create a new directory n6.. say it will be placed next to n1 for example.. it is not important all i want is that n6 should be another root directory just like n1.. and then i copy n3 to n4 and place it under n6.. but when i do only this then n7 becomes shared between n3 and n4 and when the line

n4.map.get("key").parent=n4;

executes.. the parent of n7 under n3 is also changed but i do not want it to change.. and also when i copy n3 to n4 without executing the line

n4.map.get("key").parent=n4;

the n7 under n4 sees n3 as parent..

Now all that i want to achieve is to copy the objects themselves not the handles so that when i copy a directory structure form another and paste it to another place there would not be any shared directories so that when i execute the line n4.map.get("key").parent=n4; the parent of n7 under n3 should stay as n3 and the parent of n7 under n4 should be modified to n4...


i used the clone method for
Code:

this.map=(HashMap<String, Node>) nod.map.clone();

copying the map of n3 but nothing changed..

then i used the copy constructor of hashmap class as
Code:

this.map=new HashMap<String,Node>(nod.map);

but again nothing changed in my issue..

when i try to modify the parent, item, str fields of n4 just after copying it from n3 the corresponding fields of n3 are not changed but when i try to get the node info in the hashmap and modify it then the modification is done in two places..
WHY?


i want to copy all the objects of n3 including its hashmap and the objects in the hashmap ... and if the objects of the hashmap has non-empty hashmaps i also want them to be coppied as objects not handles or references...

so that when i copy a directory info from an existing one and modify the new parent folder to the folders under the one i copied the original parent informations of subdirectories should not be changed...

and of course if n7 too has some subdirectories they would also be copied as objects , not the handles..

ANY SUGGESTIONS???

thank you...

Last edited by ishakteyran : 12-28-2007 at 01:30 PM. Reason: all the code was just a single line..
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-29-2007, 11:04 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Clone method not implemented?
Hello

It does not appear that you implemented the clone() method in the Node<V> class. When you implement the clone() method, you should create a new object and assign each original primitive type to the duplicates fields and call the clone methods of the non-primitive types. It could be that your class has inherited the clone() method from that Object class that all classes inherit from by default. That could be why your program compiles and behaves strangely.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
intimating file(s) have reached/copied in directory ashu261 Threads and Synchronization 1 04-23-2008 07:24 PM
Getting objects from a list markyoung1984 New To Java 4 03-13-2008 11:45 PM
Objects and Classes Aleve New To Java 8 12-31-2007 09:05 AM
JSP implicit objects Java Tip Java Tips 0 12-26-2007 11:12 AM
Help with Objects! Shorinhio New To Java 1 07-10-2007 10:32 PM


All times are GMT +3. The time now is 03:08 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org