簡體   English   中英

重新排序2D陣列的行和列

[英]Reordering 2D array rows and columns

我正在嘗試使用for循環對2D數組重新排序。 第一個generateSong()方法創建帶有隨機雙精度數的數組,並且可以正常工作。 然后,我有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

模擬歌曲使類似

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

您的功能有兩個問題。 首先,如果我已正確理解,您不想以任何方式更改數組,而只想逐列打印。

但是,在您的函數中有以下行:

song[i][j] = song[i][0];

這顯然使您可以更改數組。 如果您要更改數組,那么這不是正確的方法,因為您將要破壞一些值,稍后將需要通過插入新值來進行破壞。 另外,您將“ 0”指定為第二維索引,這意味着您基本上只會復制任何一行。 如果您不想更改數組,則只需刪除此行,我就看不到它對您的嘗試有何幫助。

第二個問題在這一行:

System.out.printf(" %.2f",song[i][j]);

保留現在的狀態(並刪除另一行),您將從頭到尾打印數組,但不會按列打印。 為此,您只需要反轉“ i”和“ j”即可。

這將導致此功能:

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

您可以在您的SimulationSong方法的內循環中看到...

for(int j = song[i].length-1; j >= 0; j--) {
        song[i][j] = song[i][0];
        System.out.printf(" %.2f",song[i][j]);
    }

我們將當前歌曲值[i] [j]設置為值[i] [0]。 0值永遠不變,因此您要做的就是將歌曲的每個值設置為歌曲的第一個值(0是歌曲i中的第一個值)。

因此,邏輯顯然是錯誤的。 一些需要您指導的指針:

  • 如果要切換行和列,則需要切換索引,即value [i] [j]應該變成value [j] [i],而不是[i] [0]
  • 在切換值時,您將需要一個臨時變量來存儲值。 例如,您想讓[i] [j]存儲[j] [i]的值,反之亦然,則需要將這些值之一存儲在temp中,因此您可以替換該值,然后將其移動到您剛剛在temp變量中更改的值(因為現在將在第二個變量中將其覆蓋,請參見下面的偽代碼)

     temp = row[i][j] row[i][j] = row[j][i] row[j][i] = temp 

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM