简体   繁体   English

如何检查C#中arraylist中的元素是否为空?

[英]How do you check if an element is empty in an arraylist in C#?

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? 是否可以检查位置[i]上的值是否为空? If so, how can this be solved? 如果是这样,如何解决?

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. 可以解释为空的公共值将为null ,这是对null的引用。 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. 这样,在遍历列表时,您将永远只有非空元素,并且不需要在所有地方添加一些检查来处理特殊的空值。

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. 您必须通过list.Add(somevalue)或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. 像其他人回答的那样,它可以为null。 But null is a value. 但是null是一个值。

By the way i prefer to loop a list with foreach like this 顺便说一句,我更喜欢用这样的foreach循环列表

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 因为它已经被循环设置了

It depends what your ArrayList contains. 这取决于您的ArrayList包含的内容。 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! 可能是一些语法错误,但是您明白了! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM