Reputation: 153
I am trying to move the false element in 2d array to a different blank space in element
'X*''O'' ''X''O*'
'O''O'' '' ''X'
'O'' ''X''X''O*'
'X'' '' ''X''X'
'X''X''O*''X''O*'
the elements marked with * are false elements. I want to move those false cells to look like this. In other word move all the false elements to blank space in the array
' ''O''X''X'' '
'O''O''O'' ''X'
'O''O''X''X'' '
'X''O''O''X''X'
'X''X'' ''X'' '
This is what I have so far but not getting the result, it only moves one element not all the false elements. And when I try to put break in outer loop it does not work. Any tips please, this is my code so far:
char temp = ' ';
char[][] arr = new char[tissue.length][tissue[0].length];
for (int i = 0; i < tissue.length; i++)
for (int j = 0; j < tissue[i].length; j++){
if (isSatisfied(tissue, i, j, threshold)
arr[i][j] = tissue[i][j];
if ( !isSatisfied(tissue, i, j, threshold)) {
temp = tissue[i][j];
breakloop:
for (int k = 0; k < tissue.length; k++)
for (int l = 0; l < tissue[k].length; l++)
if (tissue[k][l] == ' ') {
tissue[i][j] = arr[k][l];
arr[k][l] = temp;
break breakloop;
}
}
}
Upvotes: 0
Views: 124
Reputation: 1297
Here is a working solution. The processing is done over a single array, because it's easier to handle.
public class MoveArrayValues
{
final static String F = "X*", E = "O*";
final static String X = "X", O = "O";
final static String B = " ", T = "-";
final static int COLUMN_COUNT = 5;
public static void main( String[] args )
{
String[] source =
{
F, O, B, X, E,
O, O, B, B, X,
O, B, X, X, E,
X, B, B, X, X,
X, X, E, X, E
};
// Print the original source
for ( int ptr = 0; ptr < source.length; ptr++ )
{
System.out.print("[" + source[ptr] + "]");
if ( ptr != 0 && ((ptr + 1) % COLUMN_COUNT) == 0 ) System.out.println();
}
// Initialise the target array and then process the source
String[] target = new String[source.length];
for ( int ptr = 0; ptr < source.length; ptr++ )
{
String cell = source[ptr];
if ( cell.equals(T) )
{ // Skip cells marked as "taken"
continue;
}
if ( cell.equals(F) || cell.equals(E) )
{ // False values
target[ptr] = B; // false value becomes a blank
// now find a new location for this false value
int offset = 1;
/*
* This while loop will find the closest free cell that
* hasn't already been taken (preferring the cell to the
* right).
*
* The while condition is just to make sure we don't
* fall into an endless loop
*/
while ( offset < source.length )
{
if ( ptr + offset < source.length )
{ // Scan to the right
String tmp = source[ptr + offset];
if ( tmp.equals(B) )
{ // found a blank, now use this space
source[ptr + offset] = T; // Mark the space as "taken"
target[ptr + offset] = cell.equals(F) ? X : O;
break;
}
}
if ( ptr - offset >= 0 )
{ // Scan to the left
String tmp = source[ptr - offset];
if ( tmp.equals(B) )
{ // found a blank, now use this space
source[ptr - offset] = T; // Mark the space as "taken"
target[ptr - offset] = cell.equals(F) ? X : O;
break;
}
}
offset++;
}
}
else
{ // Normal values and spaces
target[ptr] = cell;
}
}
System.out.println("--------------------");
// Print the resultant target
for ( int ptr = 0; ptr < target.length; ptr++ )
{
System.out.print("[" + target[ptr] + "]");
if ( ptr != 0 && ((ptr + 1) % COLUMN_COUNT) == 0 ) System.out.println();
}
}
}
If you really need to work with 2d arrays, then you could either modify the outer for
loop and inner while
loop or even easier, write a little method to convert the 2d array into a single array, which is very easy.
Upvotes: 1