Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过替换数组值中的*来创建二维数组_Java - Fatal编程技术网

Java 通过替换数组值中的*来创建二维数组

Java 通过替换数组值中的*来创建二维数组,java,Java,处理二维数组时,必须使用“基于替换的开始”替换数组值 我的数组是[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 我编写的代码只是沿对角线方向更改值 f

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

我的数组是[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();
            }
        }

有什么帮助吗?

这里有一个非常通用的版本:

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

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

这是工作代码,感谢您的帮助

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

    }

你的问题很不清楚。必须使用基于替换的start替换数组值。很难理解。代码似乎不完整。你在哪里定义A,n,m,c1和c3?谢谢宫本优树!!是一个很大的帮助,我能够创建与您的代码的帮助。