在数组中移动元素

时间:2014-11-20 06:45:09

标签: java arrays multidimensional-array

我试图将2d数组中的false元素移动到元素

中的不同空格
'X*''O'' ''X''O*'
'O''O'' '' ''X'
'O'' ''X''X''O*'
'X'' '' ''X''X'
'X''X''O*''X''O*'

标有*的元素是假元素。我想把那些假细胞看成是这样的。换句话说,将所有假元素移动到数组中的空白区域

' ''O''X''X'' '
'O''O''O'' ''X'
'O''O''X''X'' '
'X''O''O''X''X'
'X''X'' ''X'' '

这是我到目前为止但没有得到结果,它只移动一个元素而不是所有的错误元素。当我试图在外部循环中放置它时,它不起作用。请提示,这是我目前的代码:

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;
                    }
        }
    }

1 个答案:

答案 0 :(得分:1)

这是一个有效的解决方案。处理是在一个阵列上完成的,因为它更容易处理。

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();
        }
    }
}

如果你真的需要使用2d数组,那么你可以修改外部for循环和内部while循环甚至更容易,编写一个小方法将2d数组转换为单个数组,这很容易。