Results 1 to 6 of 6

Thread: Problem in clone() method

  1. #1
    Join Date
    Dec 2009
    Posts
    204

    Problem in clone() method

    Hello,
    Here is my code
    Code:
    Public class Can {
     
      private Intl[][] tb = new Intl [2][2];
     
      Public Can (Intl [][] newtbau) {
        tb = newtbau;
      }
     
      Public void mdtab() {
        board[0][0] += 1;
      }
     
      Public Intl[][] getTab() {
        return tb;
      }
     
      Public void test() {
        Can tp = new Can(this.clone());
        tp.mdtab();
        System.out.System.out.println(board[0][0]+"   "+ tp.getTab()[0][0]); 
      }
    }
    So, I have problem in using the method clone(). I peeled several article on clone (), but none really solves the problem this way (and we understand why since AC does not work in). Yet AC seems logical, but with all these pointers hidden I do not know really what I handle. Thanks in advance.
    Last edited by Ash maker; 18-02-2010 at 02:24 AM.

  2. #2
    Join Date
    Nov 2009
    Posts
    583

    Re: Problem in clone() method

    Hello,
    In fact, you clone a table and that's not necessarily the top. I do not know how the method is implemented on clone tables (never used), but what I know is that your problem is there ... the values may not be copied, it is possible that this clone only the table structure in fact - so the coup, it must go through a "System.arraycopy".

  3. #3
    Join Date
    Nov 2009
    Posts
    518

    Re: Problem in clone() method

    Hello,
    If you need an example here it is

    Code:
    class Foo() {
      private Intl[][] ar = new Intl[2][2];
     
      / / Normal constructor
      Foo(){great();}
     
      / / Constructor for cloning
      Foo(Foo foo) {
        great();
        this.ar = new Intl[foo.ar.length][foo.ar[0].length];
        
        for(Intl i =0; I <ar.length; I + +) {
          for(Intl j =0J <ar[0].lengthJ + +) {
            ar[i][j] = Foo.ar[i][j];
          }
        }
      }
    }

  4. #4
    Join Date
    Nov 2009
    Posts
    359

    Re: Problem in clone() method

    Hello,
    Yes, the problem is common. The clone method works very well on objects whose attributes are primitive. If this is not the case, the clone method is called recursively on the attributes of object type (not original). Your table is a particular object, perhaps, but an object. It is likely that the clone method is not implemented. Instead we propose to use effectively "System.arraycopy. That is to say clearly you have 2 solutions almost equivalent:
    - Redefine yourself method to clone your object
    - Define a copy constructor

  5. #5
    Join Date
    Nov 2009
    Posts
    333

    Re: Problem in clone() method

    Hello,
    I do not agree, this behavior is undefined and is left to the whim of classes redefining the clone method. For example, most lists (LinkedList, ArrayList, ...), when cloning does not clone their parts (and yes ... a simple reference copy). You may be confused with the serialization, which effectively stores all non-transient attributes of objects recursively
    Just see this
    Code:
    class test {
      test(){}
      test(test foo){...}
     
      Public Object clone() {
        return new test(this);
      }
     
    }

  6. #6
    Join Date
    Nov 2009
    Posts
    446

    Re: Problem in clone() method

    Hello,
    As explained above, a table is an object, the method clone () is defined above. Beware however, the table contains literal, then the clone will have copies of values. It does not work the same way if the table contains objects (eg Integer [] or int [] []), In which case only the references are copied. I hope you are getting the point what i am trying to explain. If you have any more problem then feel free to ask here.

Similar Threads

  1. BlackBerry 8900 Clone Problem
    By Liliana in forum Portable Devices
    Replies: 6
    Last Post: 18-05-2010, 05:38 PM
  2. Problem in using bisection method.
    By KADRI in forum Software Development
    Replies: 6
    Last Post: 27-02-2010, 04:49 PM
  3. Clone method in Java
    By Joyjeet in forum Software Development
    Replies: 5
    Last Post: 16-01-2010, 01:12 AM
  4. Problem with my java method
    By manjava in forum Software Development
    Replies: 3
    Last Post: 17-11-2009, 06:15 AM
  5. Vista Ultimate dualview and clone problem
    By Greger in forum Monitor & Video Cards
    Replies: 4
    Last Post: 28-02-2009, 10:33 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,154,554.33538 seconds with 17 queries