java - 重新排序二维数组的行和列

标签 java arrays

我正在尝试使用 for 循环重新排序 2D 数组。第一个generateSong()方法创建具有随机 double 的数组并且工作正常。然后我有simulateSong() 方法。其目的是从generateSong()数组中取出行并将它们重新打印为列,从底部开始。

import java.util.concurrent.ThreadLocalRandom;

public class Guitar {

private int strings;
private int chords;

private double[][] song;

public Guitar(int mstrings, int mchords) {
    this.strings = mstrings;
    this.chords = mchords;
    song = new double[mstrings][mchords];
}

public void generateSong() {
    for (int i = 0; i < song.length; i++) {
        for (int j = 0; j < song[i].length; j++) {
            song[i][j] = ThreadLocalRandom.current().nextDouble(27.5, 4186);
            System.out.printf(" %.2f",song[i][j]);
        }
        System.out.println();
    }
}

public void simulateSong() throws InterruptedException {
    System.out.println("\nGuitar.simualateSong() ");
    for(int i = song.length-1; i >= 0; i--) {
        for(int j = song[i].length-1; j >= 0; j--) {
            song[i][j] = song[i][0];
            System.out.printf(" %.2f",song[i][j]);
        }
        System.out.println();
    }
}

}

行数和列数由 main 方法中的命令行参数设置。

public class Songwriter {

public static void main(String[] args) throws InterruptedException {

    System.out.println("Guitar(): Generated new guitar with " + args[0] + " strings. Song length is " + args[1] + " chords.");

    String args0 = args[0];
    int strings = Integer.parseInt(args0);
    String args1 = args[1];
    int chords = Integer.parseInt(args1);

    Guitar guitarObj1 = new Guitar(strings, chords);
    guitarObj1.generateSong();
    guitarObj1.simulateSong();

}

}

所以最终我想做的是使最初从左到右读取的行现在从上到下读取为列。以下是将 3 行和 4 列设置为命令行参数的预期输出。

Guitar(): Generated new guitar with 3 strings. Song length is 4 chords.
 2538.83 2269.30 1128.09 3419.77
 2356.74 2530.88 2466.83 3025.77
 3898.32 3804.22 3613.94  337.93

Guitar.simualateSong()
 3898.32 2356.74 2538.83
 3804.22 2530.88 2269.30
 3613.94 2466.83 1128.09
  337.93 3025.77 3419.77

使用我当前拥有的代码,这就是我得到的输出。

Guitar.simualateSong() 
 3898.32 3898.32 3898.32 3898.32
 2356.74 2356.74 2356.74 2356.74
 2538.83 2538.83 2538.83 2538.83

我知道唯一的问题在于simulateSong() 方法的for 循环。正如你所看到的,我的输出很接近,但没有雪茄。

最佳答案

如果我理解正确的话,应该是这样的......

public void simulateSong() {
    System.out.println("\nGuitar.simualateSong() ");
    for (int i = 0; i < chords; i++) {
        for (int j = 0; j < strings; j++) {
            System.out.printf(" %.2f", song[j][i]);
        }
        System.out.println();
    }
}

generateSong 制作类似的东西

A1 A2 A3 A4
B1 B2 B3 B4
C1 C2 C3 C4

simulateSong 制作类似的东西

A1 B1 C1
A2 B2 C2
A3 B3 C3
A4 B4 C4

关于java - 重新排序二维数组的行和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36421291/

相关文章:

java - 用Java实现矩阵结构

c# - C# 中的 TypeScript 数组等价物

java - 同步方法中的synchronized(this) block

Java( maven ): how to know where lies the error (useless backtrace)

java - Android 适配器未读取 XML 文件

c - 如何动态分配二维结构数组?

java - 如何在Eclipse中使用拉丁字母?

java - SimpleDateFormat 无法正常工作

c - 数组名称在 C 中衰减为指针 - 编译错误

java - 用 Arrays.asList 初始化 List<>