簡體   English   中英

如何在 java 的二維數組中插入一行?

[英]How do I insert a row into a 2D array in java?

用戶必須給出他們想要插入的行的索引,如下所示:

original:
2.546   3.664  2.455
1.489   4.458  3.333

insert an index row: 1
[4.222, 2.888, 7.111]

row inserted:
2.546   3.664  2.455
4.222   2.888  7.111
1.489   4.458  3.333

這是代碼:

public double[] getTheDataForRow( int len )
{
    double [] num = new double [len];
    return num;

}

public double[][] insertRow( double[][] m, int r, double[] data){
    m = new double [data.length][3];
    for(int row = 0; row<m.length; row++){
    for(int col = 0; col<m[row].length;col++)
    if(m[row][col] == r){
    m[row][col] = data;
    }
    }
    return m;
}

public void result(double[][] s){
    for(int row=0; row<s.length; row++){
        for(int col=0; col<s[0].length; c++)
            out.printf( "%5.2f", s[row][col] );
        out.println();
    }
    out.println();
}

我一直有錯誤,老實說,我不知道如何解決。 我會很感激一些幫助。

您將在insertRow的第一行破壞輸入數組m 而是創建一個新數組,該數組的大小再增加一個。 然后從輸入中復制插入點之前的所有內容。 然后插入新行。 然后從輸入中復制該行之后的所有內容(針對當前索引向后移一)。 而且,我會將方法static 喜歡,

public static double[][] insertRow(double[][] m, int r, double[] data) {
    double[][] out = new double[m.length + 1][];
    for (int i = 0; i < r; i++) {
        out[i] = m[i];
    }
    out[r] = data;
    for (int i = r + 1; i < out.length; i++) {
        out[i] = m[i - 1];
    }
    return out;
}

然后測試一下

public static void main(String[] args) {
    double[][] arr = { { 2.546, 3.664, 2.455 }, { 1.489, 4.458, 3.333 } };
    System.out.println(Arrays.deepToString(arr));
    arr = insertRow(arr, 1, new double[] { 4.222, 2.888, 7.111 });
    System.out.println(Arrays.deepToString(arr));
}

我得到(如預期)

[[2.546, 3.664, 2.455], [1.489, 4.458, 3.333]]
[[2.546, 3.664, 2.455], [4.222, 2.888, 7.111], [1.489, 4.458, 3.333]]

基於 Elliot Frish 的回答,但現在使用System.arraycopy

    private static String[][] insertRow(String[][] originalData, int numberOfRowWhereToInsertData, String[] dataToInsert) {
        String[][] output = new String[originalData.length + 1][];

        if (numberOfRowWhereToInsertData >= 0){
            System.arraycopy(originalData, 0, output, 0, numberOfRowWhereToInsertData);
        }

        output[numberOfRowWhereToInsertData] = dataToInsert;

        if (output.length - (numberOfRowWhereToInsertData + 1) >= 0){
            System.arraycopy(originalData, numberOfRowWhereToInsertData + 1 - 1, output, numberOfRowWhereToInsertData + 1, output.length - (numberOfRowWhereToInsertData + 1));
        }


        return output;

    }

並測試:

    public static void main(String[] args) {
        String[][] arr = { { "Old", "McDonald", "had" }, { "And", "on", "his" }, {"some", "cows", "Ee i ee i oh"} };
        arr = insertRow(arr, 1, new String[] { "a", "farm", "Ee i ee i o" });
        arr = insertRow(arr, 3, new String[] { "farm", "he", "had",  });
        System.out.println(Arrays.deepToString(arr));
    }

給出:

[[Old, McDonald, had], [a, farm, Ee i ee i o], [And, on, his], [farm, he, had], [some, cows, Ee i ee i oh]]

暫無
暫無

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

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