在 Java 中转置不同维度的二维数组

2024-01-07

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

这就是我到目前为止所拥有的

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

当我运行该程序时,我得到这样的信息:

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

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

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

11

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

12

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

13

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

21

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

22

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

23

11 12 13

21 22 23

0 0

21 0

0 0

所以看起来一切都很顺利(它知道转置数组的新维度),但是转置数组中的数据值并没有从原始数组中获取所有数字。


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

希望这可以帮助 :)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Java 中转置不同维度的二维数组 的相关文章

随机推荐

  • libmagic。文本/纯文本而不是文本/javascript 文本/css

    我使用 libmagic 来获取项目 Web 界面中文件的 mime 类型 我在 css 和 js 文件上得到文本 纯 mime 类型 例如 Chromium 显示以下警告 Resource interpreted as Styleshee
  • cocoa应用程序中的资源文件夹路径

    我正在开发使用一些 c 文件的 Mac OS X 应用程序 并且我有一个配置文件 我将其添加到应用程序资源中 我的问题是 资源文件夹的相对路径是什么 I tried MyAppName app Contents Resources conf
  • MATLAB 与命名空间最接近的东西是什么?

    我的实验室里有很多 MATLAB 代码 问题是确实没有办法组织它 由于所有函数都必须位于同一个文件夹中才能调用 或者您必须将一堆文件夹添加到 MATLAB 的path环境变量 似乎我们注定会在同一个文件夹中拥有大量文件 并且全部位于全局命名
  • Java应用程序中的主要方法[重复]

    这个问题在这里已经有答案了 我们到处都读到要启动一个 java 程序 我们需要一个起点 那就是静态 main 方法 在 Java EE 应用程序中 main 方法位于何处 它是否内置在应用程序服务器 容器中 它是如何触发的以及我们可以对此进
  • 从 qrc 导入 qml 时 QtCreator 语法高亮

    当我从 qrc 位于不同的目录中 导入 qml 时 它可以编译并正常工作 但是当 Qt Creator 无法识别导入的组件并且不突出显示它时 这是代码 import QtQuick 2 0 import qrc qml libs Appli
  • 根据另一列更改一列的值

    这是同样的问题根据 pandas 中的另一个值更改一个值 https stackoverflow com questions 19226488 change one value based on another value in panda
  • 带有 sortKeys 和参数值的 Spring Batch Paging

    我有一个在 Spring Boot 中运行的 Spring Batch 项目 该项目工作得很好 对于我的读者 我将 JdbcPagingItemReader 与 MySql PagingQueryProvider 结合使用 Bean pub
  • youtube-dl 速率限制下载速度和自动恢复下载[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我正在使用 shell 脚本进行视频转换 这是 shell 脚本 bin bash downloading video youtube d
  • Laravel Nova - 将 Nova 路径指向资源页面

    我需要将 Nova 路径指向资源 这样当用户登录时 他就会定向到该特定资源 我已更新此路径 config nova php path gt crm resources clients 现在登录后 我可以看到 URL 已更新 但页面仍然是We
  • 如何正确构建 KnockoutJS 应用程序

    我想知道如何以正确的方式构建 KnockoutJS 应用程序 官方文档几乎总是只使用一个 ViewModel 在仅实现了几个功能之后 我的代码变得非常混乱 并且来自面向对象的背景 我对这样的架构非常过敏 所以必须有更好的解决方案 由于对 J
  • 从 Java 调用 Mono 程序集

    我有一个用 C 编写的大型文本校对框架 我想编写一个使用这组库的 OpenOffice 扩展 我首选的语言是 Java 因此 我需要一种从 Java 访问 NET 程序集的方法 在 Windows 和Linux 有没有办法从 Java 调用
  • 程序化 MSIL 注入

    假设我有一个像这样的有缺陷的应用程序 using System namespace ConsoleApplication1 class Program static void Main string args Console WriteLi
  • RecyclerView 问题:EditText 失去焦点

    我已经放了一些EditText in RecyclerView因为我需要获得一些值 实现是这样的
  • 为什么没有换行符的情况下读取文件会更快?

    在Python 3 6中 如果存在换行符 则读取文件需要更长的时间 如果我有两个文件 一个带有换行符 另一个没有换行符 但它们具有相同的文本 那么带有换行符的文件将花费大约 100 200 的时间来读取 我已经提供了一个具体的例子 步骤 1
  • 如何让 Brew 使用 +clipboard 安装 Vim?

    我正在尝试弄清楚如何在使用 Vim 编辑的文件和 macOS 剪贴板之间进行复制和粘贴 大多数说明都说首先使用 Homebrew 安装 Vim 因为它将在启用剪贴板选项的情况下安装 vim version 将显示 clipboard 然而
  • Objective-C 调用编译器不认为存在的选择器(即使我们知道它存在)

    我在prepareForSegue方法中有这段代码 Get destination view UIViewController viewController segue destinationViewController See if it
  • TEXTMETRIC 结构和“Cambria Math”字体的问题

    如果我运行下面的代码 我会得到带有 cambria Math 字体的 tm 和 gm 结构的以下值 tm tmHeight 161 tm tmAscent 90 tm tmDescent 71 and gm gmBlackBoxY 14 中
  • 如何获得 Java 反射来查找可调用函数?

    我有一个界面ZipCodeServer哪个班级ZipCodeServerImpl实施 我也有一个界面ZipCodeList哪个班级ZipCodeListImpl实施 其中的功能之一是ZipCodeServer接口要求是 public voi
  • 有没有办法提高opencv视频处理速度? [复制]

    这个问题在这里已经有答案了 out cv2 VideoWriter output file codec fps width height while video isOpened has frame image video read if
  • 在 Java 中转置不同维度的二维数组

    嘿 我正在尝试转置一个二维数组 其行 列由用户输入 我浏览过这个网站 几乎所有我看到的建议都是针对方形数组 2x2 3x3 等 这就是我到目前为止所拥有的 import java util Scanner public class Arra