在不使用 ArrayList java 的情况下从二维数组中删除包含 null 的行

Removing rows containing null from a 2D array without using ArrayList java

我正在寻找一种解决方案来删除二维数组中包含 null 的行。这里棘手的部分是我只想使用 for 循环而没有 java.util.Arrays 或类似的帮助。 我找到了一种方法,但它仅在某些情况下有效:null 必须不在第一行或最后一行,并且 null 不得超过 2 个,否则我会得到错误的结果。

public static String[][] deleteNull(String[][]Table){
    int nbLineToDelete=0;
    int count=0;
    String [][]resizedTable = null;     
    for (int i=0; i<Table.length;i++){
            if (Table[i]==null){
                nbLineToDelete=nbLineToDelete+1;}}  
    while (count< nbLineToDelete){
    for (int i=0; i<Table.length-1;i++){
        if (Table[i] == null){
            resizedTable = new String[Table.length-1][];
            for (int index=0; index<i; index++){
                resizedTable [index]=Table[index];}
            for (int j=i; j<Table.length-1;j++){
                resizedTable [j]=Table[j+1];}
            Table[i]=resizedTable[i];}
        count=count+1;}}
    return resizedTable ;

欢迎提出任何建议。

您可以维护一个一维布尔数组,其索引 i 处的真值表示 table 的第 i 行中存在空值。 除此之外,您还可以维护一个计数器来跟踪包含空值的行数,因为它将有助于计算 resizedTable 的大小。 这是示例实现:

public class Main2 {
    public static void main(String[] args) {

        String table[][] = new String[5][5];
        for (int i = 0; i < table.length; i++) {
            for (int j = 0; j < table[i].length; j++)
                if (i == j & i < 3) {
                    table[i][j] = null;
                } else {
                    table[i][j] = "ij";
                }
        }
        System.out.println("before removing nulls----");
        for (String temp[] : table) {

            System.out.println(Arrays.toString(temp));
        }
        String res[][] = deleteNull(table);

        System.out.println("after removing nulls----");
        for (String temp[] : res) {

            System.out.println(Arrays.toString(temp));
        }
    }
public static String[][] deleteNull(String[][] table) {

    boolean flag[] = new boolean[table.length];
    int count = 0;
    for (int i = 0; i < table.length; i++) {
        if( table[i] == null)
        {
            flag[i] = true; 
            count++;
        }
        else {
            for (int j = 0; j < table[i].length; j++)
                if (table[i][j] == null) {
                    flag[i] = true;
                    count++;
                    break; // if more than 1 null, we are not bothered by that
                }
        }
    
    }
    String[][] resizedTable = new String[table.length - count][];
    int k = 0;
    for (int i = 0; i < table.length; i++) {
        if (flag[i]) {
            continue;
        } else {
            resizedTable[k] = new String[table[i].length];
            for (int j = 0; j < table[i].length; j++)
                resizedTable[k][j] = table[i][j];
        }
        k++;
    }
    return resizedTable;
}
    
    
}

输出如下:

before removing nulls----
[null, ij, ij, ij, ij]
[ij, null, ij, ij, ij]
[ij, ij, null, ij, ij]
[ij, ij, ij, ij, ij]
[ij, ij, ij, ij, ij]
after removing nulls----
[ij, ij, ij, ij, ij]
[ij, ij, ij, ij, ij]

如果整行都指向空:它也涵盖了这种情况:

before removing nulls----
null
[ij, ij, ij, ij, ij]
[ij, ij, ij, ij, ij]
[ij, ij, ij, ij, ij]
[ij, ij, ij, ij, ij]
after removing nulls----
[ij, ij, ij, ij, ij]
[ij, ij, ij, ij, ij]
[ij, ij, ij, ij, ij]
[ij, ij, ij, ij, ij]