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

Time:01-24

I´m looking for a solution to remove rows containing null in a 2 Dimensional array. The tricky part here is that i would like to use only for loop and no help from the java.util.Arrays or similar. I found out a method but it works only in some conditions: The null must be not on the first or last row and not more than 2 null muss be present otherwise I get wrong result.

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 ;

Any suggestions welcome.

CodePudding user response:

You can maintain a 1 dimensional boolean array whose true value at index i signifies that a null is present in row i of table. Along with that, you can maintain a counter that keeps track of number of rows that contain null values as it will help to calculate the size of resizedTable. This is the sample implementation :

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

and the output is as follows :

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]

In case the entire row points to null : it covers for that case too :

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]
  •  Tags:  
  • Related