03-19-2006, 10:51 AM | #1 (permalink) |
Free Mars!
Location: I dunno, there's white people around me saying "eh" all the time
|
[java]Moving values in 2d array
Ok, I got a 2d array where they are filled with certain letter. So, I need to remove any location that has the letter "X" and replace it with whatever is above.
Here what the 2D array would start out as. Notice that there are Xs in Row 12-14 that needs to be removed with whatever is above it: Code:
A B C D E F G H I J K L M 1 X X X X X X X X X X X X X 2 X X X X X X X X X X X X X 3 X X X X X X X X X X X X X 4 X X X X X X X X X X X X X 5 X X X X X X X X X X X X X 6 X X X X X X X X X X X X X 7 X X X X X X X X X X X X X 8 X X X X X X X X X X X X X 9 X X X X X X X X X X X X X 10 Y Y G Y G Y R G G G R G G 11 R R Y R G G R R Y G Y R Y 12 X Y R R R Y R R G Y R G G 13 X X R R R R Y R G R Y Y R 14 X X Y G Y R G R R Y R Y Y 15 R Y Y R G G G Y R R R G Y Code:
A B C D E F G H I J K L M 1 X X X X X X X X X X X X X 2 X X X X X X X X X X X X X 3 X X X X X X X X X X X X X 4 X X X X X X X X X X X X X 5 X X X X X X X X X X X X X 6 X X X X X X X X X X X X X 7 X X X X X X X X X X X X X 8 X X X X X X X X X X X X X 9 X X X X X X X X X X X X X 10 X X G Y G Y R G G G R G G 11 X X Y R G G R R Y G Y R Y 12 X Y R R R Y R R G Y R G G 13 Y R R R R R Y R G R Y Y R 14 R Y Y G Y R G R R Y R Y Y 15 R Y Y R G G G Y R R R G Y Code:
A B C D E F G H I J K L M 1 X X X X X X X X X X X X X 2 X X X X X X X X X X X X X 3 X X X X X X X X X X X X X 4 X X X X X X X X X X X X X 5 X X X X X X X X X X X X X 6 X X X X X X X X X X X X X 7 X X X X X X X X X X X X X 8 X X X X X X X X X X X X X 9 X X X X X X X X X X X X X 10 X X G Y G Y R G G G R G G 11 Y Y Y R G G R R Y G Y R Y 12 R R R R R Y R R G Y R G G 13 X Y R R R R Y R G R Y Y R 14 X X Y G Y R G R R Y R Y Y 15 R Y Y R G G G Y R R R G Y Code:
private void collapseRow() { for(int i = ROW_LIMITS; i > 0; i--) { for(int j = COLUMN_LIMITS; j > 0; j--) { if(grid[i][j] == "X") { if(i - 1 >= 1) { grid[i][j] = grid[i - 1][j]; grid[i - 1][j] = "X"; } } } } }
__________________
Looking out the window, that's an act of war. Staring at my shoes, that's an act of war. Committing an act of war? Oh you better believe that's an act of war |
03-19-2006, 02:26 PM | #2 (permalink) | |
Sky Piercer
Location: Ireland
|
Quote:
In your example, row 13 goes from [X, X, R, ...] -> [Y, R, R, ...] Where are the Y and R coming from? The values literally above them (row 12) are X and Y, so that's obviously not what you meant.
__________________
|
|
03-19-2006, 04:46 PM | #3 (permalink) |
Free Mars!
Location: I dunno, there's white people around me saying "eh" all the time
|
X is like a void, empty space if you will, and I need whatever is above the empty space to drop into the empty space. Kinda like the game Tetris (sp?) where you drop the blocks into the right place.
__________________
Looking out the window, that's an act of war. Staring at my shoes, that's an act of war. Committing an act of war? Oh you better believe that's an act of war |
03-20-2006, 11:19 AM | #6 (permalink) |
Free Mars!
Location: I dunno, there's white people around me saying "eh" all the time
|
I and J is suppose to end at 1 since the number on the column and letter at the top row is also part of the array. Don't ask me why, I'm just doing what my instructor told me to do.
I fixed the issue, it was pretty stupid since I was only bringing down whatever was above the row and then move on without considering the fact that the above row was also X. Code:
private void collapseRow() { for(int i = ROW_LIMITS; i > 0; i--) { for(int j = COLUMN_LIMITS; j > 0; j--) { if(grid[i][j] == "X") { for(int k = i; k > 0; k--) { if(grid[k][j] != "X") { grid[i][j] = grid[k][j]; grid[k][j] = "X"; break; } } } } } }
__________________
Looking out the window, that's an act of war. Staring at my shoes, that's an act of war. Committing an act of war? Oh you better believe that's an act of war |
04-05-2006, 01:03 PM | #7 (permalink) |
Devils Cabana Boy
Location: Central Coast CA
|
Another way to do this, better perdorming with out all the redundant writes. Travel down the array (vertically, along the i) and if the value is not x, you add it to a string (or chr array of size row_limit for speed) and delete them when you add them, then when you hit the bottom, you empty the array back up. So the first column would make an array
[y,r,r,NULL,NULL...] Keep track of the last value (index 2 in this case) and replace it as you travel back up the row. Iterate across the columns and you’re done.
__________________
Donate Blood! "Love is not finding the perfect person, but learning to see an imperfect person perfectly." -Sam Keen |
Tags |
array, javamoving, values |
|
|