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

    Default shifting a row from 2D array down one unit

    So I have this 2D array that contains some Object foo.



    [f] [f] [f][f]
    [3][3][3][3]
    [b][b][b][b]
    [x][x][x][x]
    [ ][ ][ ][ ]
    [ ][ ][ ][ ]
    [ ][ ][ ][ ]

    I want to shift all the rows down one unit, starting for the rows containing x. So the x row is first shifted to row 4. then the b row shifted to where the x row was, row 3 and so on. My question is:

    1. How can I check if the next row is empty
    2. How do I shift a row down

    Here's my attempt:

    Java Code:
       //board is the 2D array defined elsewhere
    
       public void shiftRowDown(){
    
          
           int counter = 0;
           int rowCounter = 0;
           for (int i = 0; i < this.board.length; i++) {
           for (int j = 0; j < this.board[0].length; j++){
    
               if (this.board[i][j] == Object.EMPTY) {  //an ENUM defined elsewhere ... value is empty
                 counter += 1;
                 rowCounter = i;
    
        }
              if (counter == this.board[0].length) { //if entire row is EMPTY
                
                for (int a = 1; a < rowCounter; a ++){
                this.board[rowCounter][j] = this.board[rowCounter - a][j]; //shift elements from the row before empty row down
            
              } 
           } 
       }
    }
    }

    Is this correct. Can anyone provide a psuedo code for me or provide a hint. Thanks for all help.

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

    Default Re: shifting a row from 2D array down one unit

    What type does the array hold?

    You're asking us whether the code is correct. How about you tell us? Does it compile? Does it run? Does it do what you want it to do?
    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: shifting a row from 2D array down one unit

    This is just a general scenario. I am just curious for some hint/advice on how I would first check if the next row is empty and then shift every element down 1 unit. Is my logic correct?

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

    Default Re: shifting a row from 2D array down one unit

    Please note that a two dimensional array of numbers (or whatever type of objects) is a one dimensional arrays of rows/arrays that each are one dimensional arrays of numbers (or whatever); so, there's no need to haul over each and every element; you can haul over entire rows at once, i.e. for int array[][], array[0] is the first row, array[1] is the second row etc.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: shifting a row from 2D array down one unit

    I see. So what I am doing is shifting the elements one at a time. But you are suggesting to move it by rows. Would this general implementation work?


    this.array[3][] = this.array[2][]

    The 4th row will have elements from the third row. Is this correct?

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

    Default Re: shifting a row from 2D array down one unit

    Quote Originally Posted by NoobieCode View Post
    This is just a general scenario. I am just curious for some hint/advice on how I would first check if the next row is empty and then shift every element down 1 unit. Is my logic correct?
    Again, the approach you take depends on the type of the array. What type is the array?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: shifting a row from 2D array down one unit

    Quote Originally Posted by NoobieCode View Post
    I see. So what I am doing is shifting the elements one at a time. But you are suggesting to move it by rows. Would this general implementation work?


    this.array[3][] = this.array[2][]

    The 4th row will have elements from the third row. Is this correct?
    Almost; make that: array[3]= array[2].

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: shifting a row from 2D array down one unit

    Quote Originally Posted by NoobieCode View Post
    The 4th row will have elements from the third row. Is this correct?
    What happened when you tried that?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: shifting a row from 2D array down one unit

    Oh, The example I gave was very crappy. I was just trying to understand the general implementation. Thanks for the helpful advice

Similar Threads

  1. shifting 2d array
    By lynkxs in forum New To Java
    Replies: 2
    Last Post: 03-11-2013, 07:12 PM
  2. Shifting an Array
    By Zaibatsu in forum New To Java
    Replies: 2
    Last Post: 12-14-2011, 12:02 AM
  3. Q about shifting data in an array
    By alihht in forum New To Java
    Replies: 7
    Last Post: 02-03-2010, 06:17 AM
  4. Shifting characters in array
    By Mayur in forum New To Java
    Replies: 2
    Last Post: 04-24-2009, 10:19 PM
  5. [SOLVED] Shifting an array
    By VeasMKII in forum New To Java
    Replies: 2
    Last Post: 02-04-2009, 06:18 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
  •