How to remove null values from a 2D array?

How to remove null values from a 2D array?

I'm having some trouble removing null values from a 2D array. I'm still a beginner with programming. I looked for solutions on the web, but I didn't find anything useful. 
This is and exercise at the university, so changed the name of the array just to "array", same for its objects. This is what I have:
import java.util.ArrayList;

public class Compact
{
    public static void Compact(object[][] array)
    {
        ArrayList list = new ArrayList();

        for(int i=0; i list = new ArrayList(); // creates a list to store the elements != null
            for(int j = 0; j < registrations[i].length; j++){
                if(registrations[i][j] != null){
                    list.add(registrations[i][j]); // elements != null will be added to the list.
                }
            }
            registrations[i] = list.toArray(new Student[list.size()]); // all elements from list to an array.
        }
    }


Solutions/Answers:

Answer 1:

The corresponding redimensionable structure to an Object[][] is a List<List<Object>>.

Also, note that classes in Java always start with an uppercase, by convention. It should thus be Object, and not object.

A 2D array is not really a 2D array. It's an array of arrays. There is thus one outer array, and several inner arrays. If you just want to remove nulls from the inner arrays, you can just use one temporary list to hold the elements of the current inner array during the iteration.

for(int i = 0; i < array.length; i++) {
    Object[] inner = array[i];
    List<Object> list = new ArrayList<Object>(inner.length);
    for(int j = 0; j < inner.length; j++){
        if(inner[j] != null){
            list.add(inner[j]);
        }
    }
    array[i] = list.toArray(new Object[list.size()]);
}

If the outer array contains null inner arrays, and you also want to remove these, then you shoud use a List<Object[]>:

List<Object[]> outerList = new ArrayList<Object[]>(array.length);
for(int i = 0; i < array.length; i++) {
    Object[] inner = array[i];
    if (inner != null) {
        List<Object> list = new ArrayList<Object>(inner.length);
        for(int j=0; j < inner.length; j++){
            if(inner[j] != null){
                list.add(inner[j]);
            }
        }
        outerList.add(list.toArray(new Object[list.size()]));
    }
}
array = outerList.toArray(new Object[outerList.size()][]);

Answer 2:

Just tweak your logic, Instead of making toArray at the end make it at the end of first array. Here is the code,s1 have not null values at the end

    String[][] s = new String[][] { { "00", null, null },
            { "10", "11", null }, { "20", "21", "22" } };
    String[][] s1 = new String[s.length][];
    int k = 0;

    for (int i = 0; i < s.length; i++) {
        ArrayList<Object> list = new ArrayList<Object>();
        for (int j = 0; j < s[i].length; j++) {
            if (s[i][j] != null) {
                list.add(s[i][j]);
            }
        }
        s1[k++] = list.toArray(new String[list.size()]);
    }

Answer 3:

For Each Row in the 2D array you can store the not null values in a list. So you will have a list of list containing not null values from the 2D array.
Now convert it back to a 2D array.

List<List<Integer> list = new ...  
 for(int i=0; i<array.length; i++){
               List<Integer> templist = new ... 
                for(int j=0; j < array[i].length; j++){
                    if(array[i][j] != null){
                        templist .add(array[i][j]);
                    }
                }
                if(!templist.isEmpty()){
                   list.add(templist);
                }
            }
..
//convert back to 2D array

int[][] arr2d = new int[list.length][];
for(List<Integer>:list){
 ... poulate the array accordingly
}

References

Loading...