1
ArrayList list = new ArrayList();
for (int i = 0; i < list.Count; i++)
{
    //If list[i] is empty?
}

Is it possible to check if the value on position [i] is empty? If so, how can this be solved?

6
  • 1
    just do list[i] == null Commented Apr 3, 2016 at 11:56
  • 2
    It depends on what you mean by "empty". Commented Apr 3, 2016 at 11:57
  • What type of Data is in the ArrayList? I prefer to use a list object like List<int> or List<MyClass> instead of an ArrayList. Some List Collections can contain null, others so not. Commented Apr 3, 2016 at 12:00
  • Empty = no value at all on that position. What I am trying to do is to find the first empty position in the ArrayList. Is there a better way to do this maybe? Commented Apr 3, 2016 at 12:02
  • The array consists of numbers and letters, so both ints and strings. Commented Apr 3, 2016 at 12:03

3 Answers 3

1

There is no concept of “emptyness” in lists. If you can iterate to an element in a list, that element does exist and there is some value. Now, that value could take different forms to semantically mean it’s “empty” but that really depends on your definition of “emptyness”, and on what values you assigned before to make it empty—since a list element only exists once you assign some value to it.

A common value which could be interpreted as empty would be null, which is a reference to nothing. This is the default value for all reference types so it would make some sense to use this as “empty”. But other values could be equally used here.

In general, instead of putting holes with empty values into your list, you should consider just removing those elements instead. That way, when iterating over your list, you only ever have non-empty elements, and you don’t need to add some check everywhere to handle your special empty value.

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

Comments

1
for (int i = 0; i < list.Count; i++)
{
    //If list[i] is empty?
}

Will only loop the elements that are in the array. You must assign a value to the list by either list.Add(somevalue) or list[i] == somevalue.

If it is not assigned then it does not exist at all. So a list can't have an element that is "empty" because if it is not set then there is no key or value to the element. Like the others answered it can be null though. But null is a value.

By the way i prefer to loop a list with foreach like this

foreach(var item in list) {
    // do something here.
}

That way you don't need to do

var item = list[i];

to get the value into a variable. Because it is already set by the loop

Comments

0

It depends what your ArrayList contains. And what you mean by "empty". But it shouldn't be too hard to implement. Here are some suggestions:

for(int i = 0; i<list.Count; i++){
    if(list[i] == null){         //for objects
        //do something
    }
    if(list[i].equals("")){      //for empty string
        //do something
    }
    if(list[i].exists){         //for objects you created yourselfs whit a boolean exists-function
        //do something
    }
}

Might be some syntax errors, but you get the point! :)

1 Comment

If your arraylist contains int it could test for -1 or 0, depending on your setup.

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.