Results 1 to 5 of 5
  1. #1
    joost_m is offline Member
    Join Date
    Apr 2010
    Posts
    8
    Rep Power
    0

    Unhappy store array of arrays in array of arrays

    Hi all,

    I'm having this problem with a chess program I'm writing.
    I have multiple functions where I need to store the drawing (which is an array of arrays of Pieces(eight by eight, as is a chess board), each of them instances of class Piece), in a temporary drawing, via:

    Java Code:
    temporarydraw = Drawing;
    Then I do some maths on Drawing, say try all possible moves and check if it king is being attacked after each movement, and when I'm finished doing the checks,

    Java Code:
    Drawing = temporarydraw;
    This restores the original Drawing, so I could do any moves, and get the old drawing back.
    I do this, and not just do the checks in temporarydraw itself, because I have some other functions that only work on Drawing, and don't work on any other drawing. (please don't ask me why)

    Every function that has this code in it, leave the changes I make, and don't restore the original Drawing.

    So here's my actual question: can I just save an array of arrays of Pieces into another array of arrays of pieces? (FYI: I double-checked, they are both created as an eight-by-eight array of pieces, no mistakes there)

    Please help fast, I need this project finished by tomorrow night.

  2. #2
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    Java Code:
    Pieces[][] array1 = new Pieces[8][8];
    Pieces[][] array2 = new Pieces[8][8];
    
    //do whatever you want to them
    
    array1 = array2;
    array2 = array1;
    Is that your question?

  3. #3
    joost_m is offline Member
    Join Date
    Apr 2010
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Cruncher View Post
    Java Code:
    Pieces[][] array1 = new Pieces[8][8];
    Pieces[][] array2 = new Pieces[8][8];
    
    //do whatever you want to them
    
    array1 = array2;
    array2 = array1;
    Is that your question?
    the upper part is good, the lower part is more like this:
    Java Code:
    Pieces array2[][] = new Pieces[8][8];
    Pieces array1[][] = new Pieces[8][8];
    
    array1 = array2;
    //do things with array1
    array 2 = array1;
    thanks,
    Joost

  4. #4
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    Quote Originally Posted by joost_m View Post
    the upper part is good, the lower part is more like this:
    Java Code:
    Pieces array2[][] = new Pieces[8][8];
    Pieces array1[][] = new Pieces[8][8];
    
    array1 = array2;
    //do things with array1
    array 2 = array1;
    thanks,
    Joost
    Yeah okay but i was just demonstrating that as long as they are both 2 dimensional arrays you can assigns one of them directly to the other.

  5. #5
    joost_m is offline Member
    Join Date
    Apr 2010
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Cruncher View Post
    Yeah okay but i was just demonstrating that as long as they are both 2 dimensional arrays you can assigns one of them directly to the other.
    That is the problem, my class has problems with that. Every function that does this thing, doesn't restore the original drawing. I tried to only reset the moves that I did instead of copying the whole drawing, and then it worked as it should. My conclusion: array1 = array2; doesn't work.

    The error is solved now by a workaround, but I still don't know why the original command doesn't work :(

Similar Threads

  1. Store values in arrays
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 1
    Last Post: 03-21-2010, 07:53 AM
  2. Help with arrays and array lists
    By ambernicole88 in forum New To Java
    Replies: 3
    Last Post: 12-04-2009, 09:47 PM
  3. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  4. Replies: 21
    Last Post: 11-05-2008, 02:31 PM
  5. [SOLVED] Creating an Array of Arrays?
    By xcallmejudasx in forum Advanced Java
    Replies: 5
    Last Post: 11-04-2008, 06:01 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
  •