0

How can i find out an array is empty or not, without looping?!
is there any method or anything else?

I mean, in some code like this:

string[] names = new string[5];
names[0] = "Scott";
names[1] = "jack";
names[2] = null;
names[3] = "Jones";
names[4] = "Mesut";

// or

int[] nums = new int[4];
nums[0] = 1;
// nums[1] = 2;
nums[2] = 3;
nums[3] = 4;

or some code like this:

using System;
class Example
{
    static void Main()
    {
        int size = 10;
        int counter;
        string[] str = new string[size];

        for (counter = 0; counter < size; counter++)
        {
            str[counter] = "A" + counter;
        }

        str[3] = null;

        if (counter == size)
            Console.WriteLine("Our array is full!");
        if(counter < size)
            Console.WriteLine("Our array is not full");

        for (int i = 0; i < size; i++)
        {
            Console.WriteLine(str[i]);
        }
    }
}

is there anything else for detecting an empty array without looping?

4
  • 2
    Please explain exactly what you mean by an "empty array". Do you mean an array where all items are null? Or an array without any items in it? Commented Dec 28, 2012 at 12:38
  • i mean null, or a non-initialized cell Commented Dec 28, 2012 at 12:41
  • @MesutDarvishian: You need to understand that there's no such thing as "a non-initialized cell" - an array element with a null value is just as "initialized" as an array element with a non-null value. They're both just values. Commented Dec 28, 2012 at 12:42
  • i mean, in this situation that between names[0] and names[4] just names[2] is null, how can i detect that this array has one null cell? Commented Dec 28, 2012 at 13:03

3 Answers 3

5

An array just contains a number of elements. There's no concept of an array being "empty" just because each element happens to contain the default value (0, null, or whatever).

If you want a dynamically sized collection, you should use something like List<T> instead of an array.

If you want to detect whether any element of a collection (whether that's a list, an array or anything else) is a non-default value, you have to do that via looping. (You don't have to loop in your source code, but there'll be looping involved somewhere...)

Sign up to request clarification or add additional context in comments.

3 Comments

i mean, in this situation that between names[0] and names[4] just names[2] is null, how can i detect that this array has one null cell?
@MesutDarvishian, you have to test each one until you run into null, not null, all not null, or all null. Only other way, would be to hide the array inside an other object, and set some flags as subitems are assigned, have to be special circumstances where that would pay off though.
@MesutDarvishian: I'd use LINQ: int nullElements = array.Count(x => x == null);.
2

There is no other way than looping through, even LINQ also does the looping automatically. Instead, use a list<> and check if (listName!=null && listName.Length!=0)

Hope it helps :)

Comments

0

You can use LINQ for that, to check if any element is empty in the array you just can do:

var hasNulls = myArray.Any( a => a == null );

Or if you want to select the ones with values:

var notNulls = myArray.Where( a => a != null );

1 Comment

Note that there is still a loop here. It's just that the loop is in the LINQ library instead of in the app code. (Also, you need to use default(T) so the code will work for arrays of value type, such as int[].)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.