在 Java 中转置具有不同维度的二维数组 [英] Transpose a 2d Array with varying dimensions in Java

查看:17
本文介绍了在 Java 中转置具有不同维度的二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在尝试转置一个二维数组,该数组的行/列是由用户输入的.我在这个网站上环顾四周,我看到的几乎所有建议都是针对方形阵列(2x2、3x3 等...)

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...)

这是我目前所拥有的

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:

输入行数(必须在2到5之间):2

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

输入列数(必须在2到5之间):3

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

输入 (1, 1) 的数据值:

Enter a data value for (1, 1):

11

输入 (1, 2) 的数据值:

Enter a data value for (1, 2):

12

输入 (1, 3) 的数据值:

Enter a data value for (1, 3):

13

输入 (2, 1) 的数据值:

Enter a data value for (2, 1):

21

输入 (2, 2) 的数据值:

Enter a data value for (2, 2):

22

输入 (2, 3) 的数据值:

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.

推荐答案

我只在这个地方简单一点:

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

这篇关于在 Java 中转置具有不同维度的二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆