0

I'm trying to find how many elements are in my string array, so I can add to that array from the first empty element.

Here's what I've tried to do:

int arrayLength = 0;
string[] fullName = new string[50];

if (fullName.Length > 0)
{
    arrayLength = fullName.Length - 1;
}

and then from that refer to the first available empty element as:

fullName[arrayLength] = "Test";

I can also use this to see if the array is full or not, but my problem is arrayLength is always equal to 49, so my code seems to be counting the size of the entire array, not the size of the elements that are not empty.

Cheers!

10
  • Why not using a List ? Commented Sep 8, 2015 at 9:54
  • What do you mean by "empty", containing null? Commented Sep 8, 2015 at 9:55
  • (Also, your array length isn't equal to 49, it's equal to 50, with indices from 0 to 49). You shouldn't use the name arrayLength to refer to something other than the array length, or you'll come across it later and think that's what it does mean. Commented Sep 8, 2015 at 9:56
  • But string[50] will always have 50 elements. Consider using List<string> Commented Sep 8, 2015 at 9:56
  • I'm talking about fullName having, for example, the first 5 elements with a value, and the 45 others being empty. I would like to write code that can find that the 6th element is the first empty element, if that makes sense. Commented Sep 8, 2015 at 9:57

4 Answers 4

2

you can use this function to calculate the length of your array.

private int countArray(string[] arr)
{
    int res = arr.Length;

    foreach (string item in arr)
    {
        if (String.IsNullOrEmpty(item))
        {
            res -= 1;
        }
    }

    return res;
}

EDIT : To find the first empty element

private int firstEmpty(string[] arr)
{
    int res = 0;

    foreach (string item in arr)
    {
        if (String.IsNullOrEmpty(item))
        {
            return res;
        }
        res++;
    }

    return -1; // Array is full
}
Sign up to request clarification or add additional context in comments.

Comments

1

I'm trying to find how many elements are in my string array,

array.Length

so I can add to that array from the first empty element.

Array's don't have empty elements; there's always something in there, though it could be null.

You could find that by scanning through until you hit a null, or by keeping track each time you add a new element.

If you're going to add new elements then, use List<string> this has an Add() method that will do what you want for you, as well as resizing when needed and so on.

You can likely then just use the list for the next part of the task, but if you really need an array it has a ToArray() method which will give you one.

Comments

0

So if you want to use the array instead of a list you still simply can get the number of empty elements like this:

int numberOfEmptyElements = fullName.Count(x => String.IsNullOrEmpty(x));

Comments

0

Try the below code

    string[] fullName = new string[50];

    fullName[0] = "Rihana";
    fullName[1] = "Ronaldo";

    int result = fullName.Count(i => i != null);

in result you will have the number of occupied positions. In this case 2, cause 2 arrays are filled. From there you can count the empty. :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.