检查arraylist元素等于数组 [英] check arraylist element equals an array
本文介绍了检查arraylist元素等于数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试测试元素是否等于数组。但是,即使array == arraylist [element],重复值仍然设置为false。
这是我的代码:
bool重复= false;
foreach(arraylist中的var元素)
{
if(array.Equals) (元素))
重复=真;
}
返回重复;
I am trying to test whether an element equals an array. However, even if array==arraylist[element] the value of repetitive is still set to false.
Here is my code:
bool repetitive = false;
foreach(var element in arraylist)
{
if (array.Equals(element))
repetitive = true;
}
return repetitive;
推荐答案
在新开发中永远不要使用ArrayList
。早在.NET v.2.0中,当实现泛型时,所有非泛型类都被淘汰了。他们消除了不必要的潜在危险元素类铸造使用泛型System.Collections.Generic.List<>
和其他通用集合。
你的代码没有你可以将一些元素与整个数组进行比较(如果你的数组
对象是数组;它是未声明的),它总是为假。此外,System.Equals
不适合直接通话;它的目的是非常不同的。
完全不清楚你想要实现什么(如果它甚至没有任何意义;因为它甚至不会编译因为数组
未声明和定义),但您需要的最接近的方法是任何
或此处描述的所有
扩展方法:
http://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx [<一个href =http://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspxtarget =_ blanktitle =New Window> ^ ]。
-SA
Never useArrayList
in new development. All non-generic classes were rendered obsolete as early as of .NET v.2.0, when generics were implemented. They eliminate unwanted potentially dangerous element type casting. Use genericSystem.Collections.Generic.List<>
and other generic collection.
Your code makes no sense at all, you compare some element with the whole array (if yourarray
object is array; it is undeclared), which always be false. Moreover,System.Equals
is not intended for direct call; its purpose is very different.
It's totally unclear what you are trying to achieve (if it even makes any sense at all; as is, it won't even compile becausearray
is not declared and defined), but the closest approach you would need is this one ofAny
orAll
extension methods described here:
http://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx[^].
—SA
这篇关于检查arraylist元素等于数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文