0

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;
                    }
        }
    }
3
  • 1
    Why do you create a whole new array? Why not modify the array to change the false elements to blanks? Commented Nov 20, 2014 at 6:50
  • One question, how are the new locations for the false values choosen? Closest space? or filling the array from the start? or some other way? Commented Nov 20, 2014 at 7:34
  • it can be random or the closest Commented Nov 20, 2014 at 15:22

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

you overcomplicated things, it would have been sufficient just to copy and replace the values in the new array String[] target = new String[source.length]; for (int i = 0; i < source.length; i++) { if (source[i].equals(F) || source[i].equals(E)) { target[i] = B; } else { target[i] = source[i]; } }
@MihaiC, how does your simplified code move the false value to a new location?
you are right, i misread the question. assumed op only wanted to replace false values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.