1

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.

2
  • do you want to use 2 for loop , one after the other? Commented Jan 23, 2022 at 19:49
  • it is not a must. If there is an other way to do it, I´m open for them as well. Commented Jan 23, 2022 at 20:05

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

4 Comments

Great it s doing the job. Nevertheless it seems to not work when the whole row contains null. I get error messe: Cannot read the array length because "table[i]" is null. Any suggestions?
@koursk have updated my code, please check if it works
@koursk did it work?
It´s actually working. I tried it out again. It seems to come from another function I made which fill out the whole row if a certain condition match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.