What's the quickest way to get an array's Actual length?

I know about array.length, correct me if i’m wrong;
if i declare an array like thus:

int myarray = new int[8];

Then myarray.length would always read 8, right?
What i want is to find the actual length of an array that’s in use. IE, the number of non-null elements, (or the highest non-null element?) holes in the array could be problematic here but i’ll just make sure i don’t have any.

Simplest way i can think of would be iterating backwards through the array checking if the element = null. That seems a little roundabout though, is there any better method?

If you are trying to get the non null length alot in comparison to the number of times you assign to the array, a better way would be to implement your own data class to act as storage. This will let you get the number of non-null elements in constant time.

public class CustomArray {
    int[] data;
    int count=0;
    public int this[int key] {
        get {
            return data[key];
        }
        set {
            if(value!=null&&data[key]==null) {
                count++;
            }
            else if(value==null&&data[key]!=null) {
                count--;
            }
            data[key]=value;
        }
    }
    public CustomArray(int length) {
        data=new int[length];
    }
    public int GetCount(){
        return count;
    }
}

However if you only need the non null length a few times I would just use the array as you described.

Just use a list. The moment you need resizing or have a variable number of elements in the collection then it’s time to upgrade from an array to a List.