Wex Wex - 1 year ago 59
Java Question

Function like array.length that doesn't include NULL elements?

TreeNode[] children = grid[row][col].getChildren();

I would like a simple function that can tell me how many objects are in this array?
will return an object of size no larger than 4, for example:

children[0] = null;
children[1] = TreeNode Object
children[2] = null;
children[3] = null;

Answer Source

Why don't you write it yourself:

public static <T> int getLength(T[] arr){
    int count = 0;
    for(T el : arr)
        if (el != null)
    return count;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download