简体   繁体   English

如何在 java 的二维数组中插入一行?

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

The user has to give an index of a row where they want to insert it, like this:用户必须给出他们想要插入的行的索引,如下所示:

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

here is the code:这是代码:

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

I keep having an error and I honestly do not know how to fix it.我一直有错误,老实说,我不知道如何解决。 I would appreciate some help.我会很感激一些帮助。

You are clobbering your input array m on the first line of your insertRow . 您将在insertRow的第一行破坏输入数组m Instead, create a new array of one more element in size. 而是创建一个新数组,该数组的大小再增加一个。 Then copy everything before the insertion point from the input. 然后从输入中复制插入点之前的所有内容。 Then insert the new row. 然后插入新行。 Then copy everything after that row from the input (shifted back one against the current index). 然后从输入中复制该行之后的所有内容(针对当前索引向后移一)。 And, I would make the method static . 而且,我会将方法static Like, 喜欢,

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

Then to test it, 然后测试一下

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

And I get (as expected) 我得到(如预期)

[[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]]

Building on the answer of Elliot Frish, but now with System.arraycopy :基于 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;

    }

And testing with:并测试:

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

Gives:给出:

[[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