繁体   English   中英

转置2D数组仅适用于矩形数组-Java

[英]Transposing a 2D Array Only Works For Rectangular Arrays - Java

我有一个类,该类的方法可以在给定数组,行和列的情况下转置数组

public class Transpose {

public static void main(String[] args) {
    int[][] array = new int[6][5];

    System.out.println("Original:");
    for(int i = 0; i < array.length; i++)
    {
        for(int j = 0; j < array[i].length; j++)
        {
            array[i][j] += i+1;
            System.out.print(array[i][j] + " ");
        }
        System.out.println();
    }
    System.out.println();
    transpose(array, 6, 5);
}

public static void transpose(int[][] array, int arrayRows, int arrayColumns)
{
    int[][] transposedArray = new int[arrayRows][arrayColumns];
    System.out.println("Transposed:");
    for(int i = 0; i < array.length; i++)
    {
        for(int j = 0; j < array[i].length; j++)
        {
            transposedArray[i][j] = array[j][i];
            System.out.print(transposedArray[i][j] + " ");
        }
        System.out.println();
    }
}
}

我得到的输出看起来像这样:

Original:
1 1 1 1 1 
2 2 2 2 2 
3 3 3 3 3 
4 4 4 4 4 
5 5 5 5 5 
6 6 6 6 6 

Transposed:
1 2 3 4 5 
1 2 3 4 5 
1 2 3 4 5 
1 2 3 4 5 
1 2 3 4 5 

我意识到,当arrayRowsarrayColumns的值是相同的值,例如该方法仅适用:6,6 5,5。我试图使这些值彼此相对,在列和反之亦然的行然而,这没用。 如何获得适用于非矩形/正方形数组的方法?

请参阅行注释以获取解释。 这些是我修改过的唯一行。

public static void transpose(int[][] array, int arrayRows, int arrayColumns) {
    int[][] transposedArray = new int[arrayColumns][arrayRows]; //swap number of rows and columns
    System.out.println("Transposed:");
    for (int i = 0; i < transposedArray.length; i++) { //take the length of transposedArray, not array
        for (int j = 0; j < transposedArray[i].length; j++) { //take the length of transposedArray, not array
            transposedArray[i][j] = array[j][i];
            System.out.print(transposedArray[i][j] + " ");
        }
        System.out.println();
    }
}

您需要交换的场所arrayRowsarrayColumns的转置矩阵,因为新的矩阵应该是一个[5][6]而不是[6][5]

所以你的线

int[][] transposedArray = new int[arrayRows][arrayColumns];

变成

int[][] transposedArray = new int[arrayColumns][arrayRows];

我们还需要在以下语句中交换ij ,因为循环遵循原始矩阵的索引:

transposedArray[i][j] = array[j][i];

transposedArray[j][i] = array[i][j];

最后,您无法像现在一样在创建转置矩阵时打印该矩阵,因为您只是以这种方式重新打印原始矩阵。 我建议在完成创建矩阵后将其打印出来。

经过这些更改,您的代码最终如下所示:

public static void main(String[] args) {
    int[][] array = new int[6][5];

    System.out.println("Original:");
    for(int i = 0; i < array.length; i++)
    {
        for(int j = 0; j < array[i].length; j++)
        {
            array[i][j] += i+1;
            System.out.print(array[i][j] + " ");
        }
        System.out.println();
    }
    System.out.println();
    transpose(array, 6, 5);
}

public static void transpose(int[][] array, int arrayRows, int arrayColumns)
{
    int[][] transposedArray = new int[arrayColumns][arrayRows];
    System.out.println("Transposed:");
    for(int i = 0; i < array.length; i++)
    {
        for(int j = 0; j < array[i].length; j++)
        {
            transposedArray[j][i] = array[i][j];
        }
    }
    for(int i = 0; i < transposedArray.length; i++) { //print the transposed matrix
        for(int j = 0; j < transposedArray[i].length; j++) {
            System.out.print(transposedArray[i][j] + " ");
        }
        System.out.println();
    }
}

您可以在此处看到一个有效的示例。

for(int i = 0; i < array.length; i++) //应该计算行数。 =从0 ... 5迭代6

for(int j = 0; j < array[i].length; j++) // should count the number of columns = 5

从0..4迭代

transposedArray[i][j] = array[j][i];

访问6 ...您需要j = 5; i = 0 ... 4您的循环无法访问值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM