Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many elements of array are not null?

Tags:

An array is defined of assumed elements like I have array like String[] strArray = new String[50];.

Now from 50 elements only some elements are assigned and remaining are left null then I want the number of assigned elements.

Like here only 30 elements are assigned then I want that figure.

like image 768
Harikrishna Avatar asked Mar 06 '10 07:03

Harikrishna


People also ask

How can you tell if an array element is not null?

To check if an array is null, use equal to operator and check if array is equal to the value null. In the following example, we will initialize an integer array with null. And then use equal to comparison operator in an If Else statement to check if array is null. The array is empty.

Does array length count null values?

This function counts all members, including nulls. An empty array (ARRAY[]) has a length of 0.

Can an array have null elements?

An array value can be non-empty, empty (cardinality zero), or null. The individual elements in the array can be null or not null.

Can array have null values C?

Arrays in C don't have null slots. Using it as if it were an array of pointers to structs.


1 Answers

You can use Enumerable.Count:

string[] strArray = new string[50]; ... int result = strArray.Count(s => s != null); 

This extension method iterates the array and counts the number of elements the specified predicate applies to.

like image 195
dtb Avatar answered Sep 23 '22 21:09

dtb