有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

处理二维数组时,必须使用“基于替换的开始”替换数组值

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

例如

* 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) 个答案

  1. # 1 楼答案

    这是工作代码,谢谢你的帮助

    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();
            }
            }
    
        }
    
  2. # 2 楼答案

    以下是一个高度概括的版本:

    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"));
    

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