如何将整数的一维数组打印到二维数组中

时间:2018-11-21 15:01:08

标签: java arrays

假设我采用一维整数数组

A = {1,2,3,4,5,6,7,8, 9, 10, 11, 12}

现在,我想以对角线方式用p行和q列在二维数组中重新排列A中的整数。其中,p = 3并且 q = 4。

输出将如下所示:

1 2 4 7
3 5 8 10
6 9 11 12

3 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

1) Initialize a 2d array of the specified dimension, i.e p by q
2) For each cell in the first row, fill its anti-diagonal ( like from north-east to south-west direction) with the next available elements in your 1d array.
3) After the first row is processed, similarly do step 2 for each remaining cell in the last column of the 2d array.

最后,您的2d数组将包含您想要的答案(即数字以对角线形式填充)

答案 1 :(得分:0)

要获得此结果,您可以这样做:

int z = 0;
for(int i = 0; i < 3; i++){
    for (int j = 0; j < 4; j++){
       System.out.print(A[z]);
       z++;
    }
   System.out.println();
}

这使用嵌套的for循环来遍历ij,相当于pq行和列。内部循环包括一个z计数器,它是原始数组中的索引。确保在代码中包含A[]的定义。

答案 2 :(得分:0)

您可以这样做,

public class Main {

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
        int[][] diagonalArray = createDiagonalArray(array, 3, 4);
        print2DArray(diagonalArray);
    }

    private static int[][] createDiagonalArray(int[] array, int p, int q) {

        int[][] input = new int[p][q];
        for (int j = 0; j < p; j++) {
            for (int i = 0; i < q; i++) {
                input[j][i] = array[j * q + i];
            }
        }

        final int numRows = input.length;
        final int numColumns = input[0].length;
        int[][] result = new int[numRows][numColumns];

        int rowIndex = 0;
        int columnIndex = 0;
        int currentRow = 0;
        int currentColumn = 0;
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < numColumns; j++) {
                result[currentRow][currentColumn] = input[i][j];
                if (currentRow == numRows - 1) {
                    if (numRows < numColumns && columnIndex < numColumns - 1) {
                        currentRow = 0;
                        currentColumn = ++columnIndex;
                    } else {
                        currentRow = ++rowIndex;
                        currentColumn = numColumns - 1;
                    }
                } else if (currentColumn == 0) {
                    if (columnIndex < numColumns - 1) {
                        currentRow = rowIndex;
                        currentColumn = ++columnIndex;
                    } else {
                        currentColumn = columnIndex;
                        currentRow = ++rowIndex;
                    }
                } else {
                    currentRow++;
                    currentColumn--;
                }

            }
        }
        return result;
    }

    private static void print2DArray(int[][] diagonalArray) {
        for (int j = 0; j < 3; j++) {
            for (int i = 0; i < 4; i++) {
                System.out.print(diagonalArray[j][i] + " ");
            }
            System.out.println();
        }
    }
}

二维部分取自here

相关问题