say i have a class type and I have to declare an array to store this type. E.g.:
MyType[] t = new MyType[5]
This means I allocate 5 cells to store items of MyType. And inside my program let's say I added 2 MyType items (dynamically)
t[0] = new MyType(..);
t[1] = new MyType(..);
If i want to find out how many items are actually filled,
int count=0;
for(int j =0; j<t.length ; j++){
if ( t[j] != null ){
++count;
}
}
Is testing for null the correct way ? as i couldn't seem to increment count. count is always 0. Also, length() shows the size of the array which is 5. Is there some method to show the count of actual filled items? thanks
length()instead oflength.