Java 如何从二维数组中删除行?

Java 如何从二维数组中删除行?,java,arrays,multidimensional-array,remove,Java,Arrays,Multidimensional Array,Remove,我有一个简单的数组,有点像这样: 1 2 3 4 5 6 7 8 9 6 2 7 2 9 6 8 10 5 2 6 4 7 8 4 3 2 5 9 8 7 5 9 7 4 1 10 5 3 6 8 2 7 3 7 2 那么,让我们称之为矩阵[5][9]。现在我想删除这个矩阵中包含某个值的每一行,在本例中是10,所以我只剩下 1 2 3 4 5 6 7 8 9 2 6 4 7 8 4 3 2 5 5 3 6 8 2 7 3 7 2 不能从Java内置数组数据结构中删除元素。您必须创建一个长度小

我有一个简单的数组,有点像这样:

1 2 3 4 5 6 7 8 9
6 2 7 2 9 6 8 10 5
2 6 4 7 8 4 3 2 5
9 8 7 5 9 7 4 1 10
5 3 6 8 2 7 3 7 2
那么,让我们称之为
矩阵[5][9]
。现在我想删除这个矩阵中包含某个值的每一行,在本例中是
10
,所以我只剩下

1 2 3 4 5 6 7 8 9
2 6 4 7 8 4 3 2 5
5 3 6 8 2 7 3 7 2

不能从Java内置数组数据结构中删除元素。您必须创建一个长度小于第一个数组的新数组,并将除要删除的数组外的所有数组复制到该数组中。

使用或使用代替数组
ArrayList
具有快速访问随机元素和缓慢的
remove
方法,与
LinkedList
相反。你必须自己选择。

这是一个你可以运行的示例类,我相信它能满足你的需要。从2D数组中删除行是一件棘手的事情,因为正如@KalebBrasee所说的,您不能真正“删除”它们,而是必须创建一个全新的2D数组。希望这有帮助

import java.util.ArrayList;
导入java.util.List;
公共类矩阵{
私人双[]数据;
公共矩阵(双[]数据){
int r=数据长度;
int c=数据[0]。长度;
this.data=新的双[r][c];
对于(int i=0;i
我的目标:

import java.util.Arrays;

public class RemoveArrayRow {
    private static <T> T[] concat(T[] a, T[] b) {
        final int alen = a.length;
        final int blen = b.length;

        if (alen == 0) {
            return b;
        }

        if (blen == 0) {
            return a;
        }

        final T[] result = (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), alen + blen);

        System.arraycopy(a, 0, result, 0, alen);
        System.arraycopy(b, 0, result, alen, blen);

        return result;
    }

    public static void main(String[] args) {
        double[][] d  = { {11, 2, 3, 4, 5, 6, 7, 8, 9, 0},
                          {12, 2, 3, 4, 5, 6, 7, 8, 9, 1},
                          {13, 2, 3, 4, 5, 6, 7, 8, 9, 2},
                          {14, 2, 3, 4, 5, 6, 7, 8, 9, 3},
                          {15, 2, 3, 4, 5, 6, 7, 8, 9, 4} };

        //remove the fourth row:

        // (1)
        double[][] d1 = concat(Arrays.copyOf(d, 3), Arrays.copyOfRange(d, 4, 5));

        // (2)
        double[][] d2 = new double[d.length - 1][d[0].length];
        System.arraycopy(d, 0, d2, 0, 3);
        System.arraycopy(d, 4, d2, 3, 1);

        System.out.print(d1.length);
        System.out.print(d2.length);
    }
}
导入java.util.array;
公共类RemoveArrayRow{
私有静态T[]concat(T[]a,T[]b){
最终int alen=a.长度;
最终整数blen=b.长度;
如果(alen==0){
返回b;
}
如果(blen==0){
返回a;
}
最终的T[]结果=(T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(),alen+blen);
数组复制(a,0,result,0,alen);
数组副本(b,0,result,alen,blen);
返回结果;
}
公共静态void main(字符串[]args){
double[]d={{11,2,3,4,5,6,7,8,9,0},
{12, 2, 3, 4, 5, 6, 7, 8, 9, 1},
{13, 2, 3, 4, 5, 6, 7, 8, 9, 2},
{14, 2, 3, 4, 5, 6, 7, 8, 9, 3},
{15, 2, 3, 4, 5, 6, 7, 8, 9, 4} };
//删除第四行:
// (1)
double[]d1=concat(Arrays.copyOf(d,3),Arrays.copyOf范围(d,4,5));
// (2)
double[]d2=新的双精度[d.length-1][d[0].length];
系统阵列副本(d,0,d2,0,3);
系统阵列副本(d,4,d2,3,1);
系统输出打印(d1.长度);
系统输出打印(d2长度);
}
}
(1)

如果排除用于连接两个数组的
concat()
函数,则它将在一行中完成:
double[]d1=concat(Arrays.copyOf(d,3),Arrays.copyOf范围(d,4,5))
我也看到了。这就是
concat()
函数的代码来源

(2)


此方法速度更快,只使用现有的函数。

此时,您必须重新创建阵列并丢弃旧的阵列。无法更改现有数组的维度-如果需要这种类型的数据结构,则应基于集合构建矩阵(
ArrayList
),在那里可以轻松删除行

回到数组-其思想是收集所有要保留的行(双[]数组),用这些行创建一个结果数组,并用矩阵上的新行替换旧行:

public void doSomethingWith(Matrix in) {
  List<double[]> survivingRows = new ArrayList<double[]>();
  for (double[] row:in.getRows()) {
    if (isAGoodOne(row)) {
      survivingRows.add(row);
    }
  }

  double[][] result = new double[survivingRows][];
  for (int i = 0; i < result.length; i++) {
    result[i] = survivingRows.get(i);
  }
  in.setArray(result);
}
public void doSomethingWith(矩阵中){
列出幸存行
public Matrix removeRows(Matrix input) {
    int[][] output = new int[input.numRows][input.numColumns]();
    int i = 0;
    for (int[] row : input.rows()) {      // Matrix.rows() is a method that returns an array of all the rows in the matrix
        if (!row.contains(10)) {
            output[i] = row;
        }
    }
    return output
public static int[][] remove(int[][] a, int v) {
    int r = a.length;
    int c = a[0].length;

    int[][] b = new int[r][c];

    int red = 0;
    boolean s = false;
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            b[i - red][j] = a[i][j];
            if (a[i][j] == v) {
                red += 1;
                if(i==r-1){
                    s = true;
                }
                break;
            }
        }
    }
    //check last row
    if(s){
    for(int i = r-red;i <r-red +1; i++ )
        for (int j = 0; j<c; j++){
            b[i][j] = 0;
        }
    }
    return b;
}

public static void main(String[] args){
    int[][] a = { {1, 2, 3, 4, 5, 6, 7, 8, 1},
            {6, 2, 7, 2, 9, 6, 8, 10, 5},
            {2, 6, 4, 7, 8, 4, 2, 2, 5},
            {9, 8, 7, 5, 9, 7, 4, 1, 1},
            {5, 3, 6, 8, 2, 7, 3, 1, 1} };

    print(remove(a, 10));


}

public static void print(int[][] a) {
    int r = a.length;
    int c = a[0].length;


    int red = 0;
    for (int i = 0; i < r; i++) {
        System.out.printf("\nrow %d, \n", i);
        for (int j = 0; j < c; j++) {
            System.out.printf("%d, ", a[i][j]);
        }
    }
}
String[][] src = getSheetData(service, spreadSheetId, range);
String[][] dest = new String[src.length-1][src[0].length];

for (int i = 1; i < src.length; i++) {
System.arraycopy(src[i], 0, dest[i-1], 0, src[0].length-1);
}
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 6, 4, 7, 8, 4, 3, 2, 5]
[5, 3, 6, 8, 2, 7, 3, 7, 2]