Transpose a 2d Array with varying dimensions in Ja

2019-09-15 19:21发布

问题:

Hey I'm trying to transpose a 2d Array who's rows/columns are inputted by the user. I've looked around on this site and pretty much all the advice I see is for square arrays (2x2,3x3, etc...)

this is what I have so far

import java.util.Scanner;

public class ArrayTranspose {

public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);

    System.out.print("Input the number of rows (must be between 2 and 5): ");
    int rows = kb.nextInt();
    if ((rows < 2) && (rows > 5)) {
        System.out.println("Error: range must be between 2-5");
        rows = -1;
    }
    System.out.print("Input the number of columns (must be between 2 and 5): ");
    int cols = kb.nextInt();
    if ((cols < 2) && (cols > 5)) {
        System.out.println("Error: range must be between 2-5");
        cols = -1;
    }

    int myArray[][] = new int[rows][cols];

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            System.out.println("Enter a data value for (" + (i + 1) + ", " + (j + 1) + "): ");
            int value = kb.nextInt();
            myArray[i][j] = value;
        }
    }
    printArray(myArray);
    System.out.println();
    int newArray[][] = transpose(myArray);
    printNewArray(newArray);
}

public static void printArray(int myArray[][]) {
    int dim1 = myArray.length; // Gets the number of rows
    int dim2 = myArray[0].length; // Gets the number of columns

    for (int i = 0; i < dim1; i++) {
        for (int j = 0; j < dim2; j++) {
            System.out.printf("%4d", myArray[i][j]);
        }
        System.out.println();
    }
}

public static int[][] transpose(int myArray[][]) {
    int newArray[][] = new int[myArray[0].length][myArray.length];

    for (int i = 0; i < myArray.length; i++) {
        for (int j = 0; j < i; j++) {
            // swap element[i,j] and element[j,i]
            int temp = myArray[i][j];
            newArray[i][j] = temp;
        }
    }
    return newArray;
}

public static void printNewArray(int myArray[][]) {
    int dim1 = myArray.length; // Gets the number of rows
    int dim2 = myArray[0].length; // Gets the number of columns

    for (int i = 0; i < dim1; i++) {
        for (int j = 0; j < dim2; j++) {
            System.out.printf("%4d", myArray[i][j]);
        }
        System.out.println();
    }
}
}

and when I run the program I get something like this:

Input the number of rows (must be between 2 and 5): 2

Input the number of columns (must be between 2 and 5): 3

Enter a data value for (1, 1):

11

Enter a data value for (1, 2):

12

Enter a data value for (1, 3):

13

Enter a data value for (2, 1):

21

Enter a data value for (2, 2):

22

Enter a data value for (2, 3):

23

11 12 13

21 22 23

0 0

21 0

0 0

So it seems like everything is going well (it knows the new dimensions for the transposed array) but the data values in the transposed array dont take all the numbers in from the original array.

回答1:

    public static int[][] transpose(int myArray[][]) {
    int newArray[][] = new int[myArray[0].length][myArray.length];

    for (int i = 0; i < myArray[0].length; i++) {
        for (int j = 0; j < myArray.length; j++) {
            int temp = myArray[j][i];
            newArray[i][j] = temp;
        }
    }
    return newArray;
}

Hope this Helps :)



回答2:

I make it simple a little bit only in this place:

public static int[][] transpose(int myArray[][]) {
    int newArray[][] = new int[myArray[0].length][myArray.length];

    for (int i = 0; i < newArray.length; i++) {
        for (int j = 0; j < newArray[i].length; j++) {
            newArray[i][j] = myArray[j][i];
        }
    }
    return newArray;
}