通过替换数组值中的*创建2D数组

时间:2019-03-06 10:10:07

标签: java

处理2D数组时,必须基于替换以开始替换数组值。

我的数组是[A B C D],它必须通过用数组中的*替换1,2,3和4个字符来创建所有可能的组合。

对于EG

* B C D
A * C D
A B * D
A B C *  //Replacing 1
* * C D
A * * D
A B * * //Replacing 2
* * * D
A * * *
* B * *
* * C * //Replacing 3
* * * * //Replacing 4

我编写的代码只是沿对角线更改值。

for(int i=0; i<n; i++)
            {
                for(int j=0; j<m; j++)
                {
                    // Filling the diagonals with second character
                    //if(i==j || (i+j)==(n-1))  
                    if(i==j)
                    A[i][j] = c3;                   
                    else // Filling all other positions with second character
                        A[i][j] = c1; 
                }
            }

            for(int i=0; i<n/2; i++)
            {
                for(int j=i+1; j<m-1-i; j++)
                {
                    // Filling the upper positions.
                    A[i][j] = c1; 

                    // Filling the lower positions.
                    A[n-1-i][j] = c1; 
                }
            }

            // Printing the Matrix
            System.out.println("\nOutput : \n");
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<m; j++)
                {
                    System.out.print(A[i][j]+" ");
                }
                System.out.println();
            }
        }

有什么帮助吗?

2 个答案:

答案 0 :(得分:1)

这是一个高度概括的版本:

public static char[][] row2array(char... row)
{
    // row size
    int n = row.length;
    // row count in result
    int m = 1 << n;
    // result two dimensional array
    char[][] result = new char[m][n];
    // outer loop: rows
    for (int i = 0; i < m; ++i) {
        // inner loop: columns
        for (int j = 0; j < n; ++j) {
            // condition: is the bit set?
            if ((i & (1 << j)) > 0) {
                // if yes, then replace with asterisk
                result[i][j] = '*';
            } else {
                // otherwise just add the element from the row
                result[i][j] = row[j];
            }
        }
    }
    // finished
    return result;
}

System.out.println(Arrays.deepToString(row2array('A', 'B', 'C', 'D')).replace("],", "],\n"));

这使用行索引的位来指定要替换的字符...

答案 1 :(得分:0)

这是工作代码,谢谢您的所有帮助!

public class shiftstar {
    public static void main(String args[])
    {   
        char colm[]= {'A','B','C','D','E','F','G','H','I'};
        int col = colm.length; //Number of Characters as column
        int row = 1 << col;  //Total Number of Rows
        // result two  array
        char[][] result = new char[row][col];
        // outer loop: rows
        for (int i = 0; i < row; ++i) {
            // inner loop: columns
            for (int j = 0; j < col; ++j) {
                // condition: is the bit set?
                if ((i & (1 << j)) > 0) {
                    // if yes, then replace with asterisk
                    result[i][j] = '*';
                } else {
                    // otherwise just add the element from the row

                    result[i][j] = colm[j];
                }
            }
            }
        System.out.println("\nOutput : \n");
        for(int i=0; i<row; i++)
        {
            for(int j=0; j<col; j++)
            {
                System.out.print(result[i][j]+" ");
            }
            System.out.println();
        }
        }

    }