如何切片一个二维Java数组?

时间:2021-10-02 15:39:29

folks,

伙计们,

I am working with JTables and have a 2-D array. I need to remove the first element of every row in the array. Is there a simpler way to do it besides?

我正在使用JTables和一个二维数组。我需要删除数组中每一行的第一个元素。还有更简单的方法吗?

int height = data2.length;
int width = data2[0].length;
Object[][] data = new Object[height][width];
for (int j=0; j<height; j++) {
    for (int i=1; i<width; i++) {
        data[j][i-1] = data2[j][i];
    }
}
data2 = data;

Thanks for your time!

谢谢你的时间!

1 个解决方案

#1


3  

Yes - you can use System.arraycopy instead of the inner loop:

是的,你可以使用系统。arraycopy而不是内部循环:

Object[][] data = new Object[height][width];
for (int i = 0; i < height; i++) {
    System.arraycopy(data2[i], 1, data[i], 0, width-1);
}

#1


3  

Yes - you can use System.arraycopy instead of the inner loop:

是的,你可以使用系统。arraycopy而不是内部循环:

Object[][] data = new Object[height][width];
for (int i = 0; i < height; i++) {
    System.arraycopy(data2[i], 1, data[i], 0, width-1);
}