检查arraylist元素等于数组

时间:2015-01-08 17:57:31

标签: c# arrays arraylist

我正在尝试测试元素是否等于数组。但是,即使array==arraylist[element] repetitive的值仍设置为false,也是如此。

这是我的代码:

bool repetitive = false;
foreach(var element in arraylist)
{
    if (array.Equals(element))
        repetitive = true;
}
return repetitive;

1 个答案:

答案 0 :(得分:1)

您需要在找到重复值后返回true或执行or equals

返回true方法:

bool repetitive = false;
foreach(var element in arraylist)
{
    if (array.Equals(element))
        return true;  //<<< This is the difference
    else
        repetitive = false;
}
return repetitive;

或使用按位或等于

bool repetitive = false;
foreach(var element in arraylist)
{
    if (array.Equals(element))
        repetitive |= true;
    else
        repetitive |= false;
}
return repetitive;