Results 1 to 2 of 2
  1. #1
    ishakteyran is offline Member
    Join Date
    Dec 2007
    Posts
    11
    Rep Power
    0

    Default how can objects themselves be copied???

    Code:
    Java 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..

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default 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. :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

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

Posting Permissions

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