Results 1 to 8 of 8
  1. #1
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default deep copy of an object containing 2D array and turn [CHESS]

    Hi. I have a question about deep copying an object. I have an object called chessBoard and it stores 2 things: 1. 2D array of board containing Enums and 2. turn . I want to know if I am implementing deep copy correctly.

    Java Code:
    public class chessBoard{
    
    private Chess[][] board;
    //false represents white turn
    boolean turn = false;
    
    
    //constructor
    public chessBoard(){
    
    this.turn = false;
    board = new Chess[8][8];
    
    for (int i =0; i < 8; i++){
        for (int j = 0; j < 8; j++){
    	
    	board[i][j] = Chess.EMPTY //enum 
      }
     }
    
    
    }
    
    
    
    /**
    *
    *QUESTION: am I implementing a deep copy of this object correctly?
    * The object itself has to be a deep copy, Since the 2D array is of enums, i dont think i need to deep copy thath
    * Is this correct implementation?
    */
    public chessBoard (chessBoard other) {
    
    
    this.turn = other.turn;
    
    board = new Chess[8][8];
    
    for (int i =0; i < 8; i++){
        for (int j = 0; j < 8; j++){
    	
    	board[i][j] = other.board[i][j];
      }
     }
    
    
    }
    Thanks

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: deep copy of an object containing 2D array and turn [CHESS]

    What happens if I get one of the instances of Chess that the original board refers to and modify it? For example, maybe the Chess instance has an associated color? It'll change in both instances of chessBoard, which is probably not what you want.

    Also, you should really use standard naming conventions. Classes and constructors start with an upper-case letter. Variables and methods start with a lower-case letter.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: deep copy of an object containing 2D array and turn [CHESS]

    ok. I was wondering how I can make it so that my copy constructor creates an independent object of its own where if I modify one, it wont modify the other.

  4. #4
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: deep copy of an object containing 2D array and turn [CHESS]

    A friend told me arrays of enums don't need deep copies. So I don't understand how to deep copy the ChessBoard object (fixed name)

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: deep copy of an object containing 2D array and turn [CHESS]

    You did not post an SSCCE, so we have no idea what Chess actually is. My advice assumes it's a mutable object. If you want more specific help, post an SSCCE using standard naming conventions.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: deep copy of an object containing 2D array and turn [CHESS]

    Yep, that's fine (you forgot a semi colon at the end of line #17).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: deep copy of an object containing 2D array and turn [CHESS]

    Thanks all

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: deep copy of an object containing 2D array and turn [CHESS]

    That's because they are implicitly static. Imagine a static array of type int. Regardless of how many levels of indirection that exist (i.e. arrays of arrays, etc), changing the contents of any level will be seen in all the copies.

    But you still need to copy your board, square by square, and not just copy the reference.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Deep Copy Constructor
    By tngo in forum New To Java
    Replies: 3
    Last Post: 01-29-2013, 02:58 PM
  2. deep copy in java
    By kmm1977 in forum New To Java
    Replies: 1
    Last Post: 12-09-2011, 05:59 PM
  3. [SOLVED] Deep copy
    By thorne_ in forum New To Java
    Replies: 5
    Last Post: 06-05-2009, 03:51 PM
  4. Deep copy?!
    By deepthought015 in forum New To Java
    Replies: 3
    Last Post: 05-07-2009, 06:31 PM
  5. Deep Copy Test
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:05 PM

Tags for this Thread

Posting Permissions

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