Results 1 to 9 of 9
 02242014, 07:17 PM #1Senior Member
 Join Date
 Mar 2013
 Posts
 123
 Rep Power
 0
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.
 02242014, 07:30 PM #2
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!
 02242014, 07:32 PM #3Senior Member
 Join Date
 Mar 2013
 Posts
 123
 Rep Power
 0
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?
 02242014, 07:33 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,771
 Blog Entries
 7
 Rep Power
 21
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,
Joscenosillicaphobia: the fear for an empty beer glass
 02242014, 07:37 PM #5Senior Member
 Join Date
 Mar 2013
 Posts
 123
 Rep Power
 0
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?
 02242014, 07:43 PM #6
Re: shifting a row from 2D array down one unit
How to Ask Questions the Smart Way
Static Void Games  Play indie games, learn from game tutorials and source code, upload your own games!
 02242014, 07:43 PM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,771
 Blog Entries
 7
 Rep Power
 21
 02242014, 07:44 PM #8
Re: shifting a row from 2D array down one unit
How to Ask Questions the Smart Way
Static Void Games  Play indie games, learn from game tutorials and source code, upload your own games!
 02242014, 07:49 PM #9Senior Member
 Join Date
 Mar 2013
 Posts
 123
 Rep Power
 0
Similar Threads

shifting 2d array
By lynkxs in forum New To JavaReplies: 2Last Post: 03112013, 08:12 PM 
Shifting an Array
By Zaibatsu in forum New To JavaReplies: 2Last Post: 12142011, 01:02 AM 
Q about shifting data in an array
By alihht in forum New To JavaReplies: 7Last Post: 02032010, 07:17 AM 
Shifting characters in array
By Mayur in forum New To JavaReplies: 2Last Post: 04242009, 11:19 PM 
[SOLVED] Shifting an array
By VeasMKII in forum New To JavaReplies: 2Last Post: 02042009, 07:18 PM
Bookmarks