# shifting a row from 2D array down one unit

• 02-24-2014, 07:17 PM
NoobieCode
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:

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.
• 02-24-2014, 07:30 PM
KevinWorkman
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?
• 02-24-2014, 07:32 PM
NoobieCode
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?
• 02-24-2014, 07:33 PM
JosAH
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
• 02-24-2014, 07:37 PM
NoobieCode
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?
• 02-24-2014, 07:43 PM
JosAH
Re: shifting a row from 2D array down one unit
Quote:

Originally Posted by NoobieCode
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
• 02-24-2014, 07:43 PM
KevinWorkman
Re: shifting a row from 2D array down one unit
Quote:

Originally Posted by NoobieCode
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?
• 02-24-2014, 07:44 PM
KevinWorkman
Re: shifting a row from 2D array down one unit
Quote:

Originally Posted by NoobieCode
The 4th row will have elements from the third row. Is this correct?

What happened when you tried that?
• 02-24-2014, 07:49 PM
NoobieCode
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