簡體   English   中英

通過替換數組值中的*創建2D數組

[英]create 2D array by replacing * in array values

在二維數組上工作時,必須基於替換以start替換數組值。

我的數組是[ABCD],它必須通過用數組中的*替換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();
            }
        }

有什么幫助嗎?

這是一個高度概括的版本:

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

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM