【问题标题】:How Can I print one dimension array of integer into two dimensional array如何将整数的一维数组打印成二维数组
【发布时间】:2018-11-21 15:01:08
【问题描述】:

假设,我取一个整数的一维数组

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

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

输出将是这样的:

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

【问题讨论】:

  • 到目前为止你有什么尝试?
  • 那么你尝试了什么?
  • 我不明白给定输入的 p=3 和 q=4 应该如何为您提供该输出。它似乎没有遵循我可以看到的任何逻辑顺序。它包含 3x4 网格中的所有数字,但顺序似乎相当随机。
  • @Michael,数字总是以反对角线方式填充

标签: java arrays


【解决方案1】:

你可以这样做,

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

【讨论】:

  • 谢谢,这就是我要找的。​​span>
【解决方案2】:

您可以尝试以下方法:

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.

最后,您的二维数组将保存您想要的答案(即以反对角方式填充的数字)

【讨论】:

    【解决方案3】:

    要得到这个结果,你可以这样做:

    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[] 的定义。

    【讨论】:

    • 欢迎来到 SO,Giuseppe D.!此处不鼓励仅使用代码的答案,因为它们无法深入了解问题是如何解决的。请更新您的解决方案,以说明您的代码如何解决手头的问题:)
    猜你喜欢
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多