将值与数组值进行比较 [英] compare avalue with an array values
本文介绍了将值与数组值进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hiii全部
我有一个像这样的数组:
hiii all
i have an array like that:
<br />
int[] Departments = new int[4] { 2, 3, 4, 5 };<br />
并且我有另一个由用户输入的值.我想检查此值是否在数组值中.
我尝试过:
and i have another value entered by the user. i want to check if this value is in the array values or not.
i tried :
<br />
<pre lang="midl">if(x "Not IN" Departments)<br />
{<br />
}</pre><br />
<br />
但其中有一个错误.
请问有人可以帮助我吗?
谢谢
but there is an error in it.
can any body help me in that, please???
Thanks
推荐答案
尝试:
int[] Departments = new int[4] { 2, 3, 4, 5 };
if (Departments.Contains(3))
{
MessageBox.Show("Yes!");
}
除了上述解决方案之外,您还可以尝试BinarySearch
检查给定数组中是否存在值.
看看
In addition to above solution you can also tryBinarySearch
for checking value is present or not in given set of array.
take a look
int[] Departments = new int[4] { 2, 3, 4, 5 };
if (Array.BinarySearch(Departments, 3) >= 0)
{
//Yes Present
}
当然,如果要检查"not in",并使用上面的示例Griff,它将是:
Of course, if you want to check for "not in", and using Griff''s example above, it would be:
if (!Departments.Contains(3))
{
MessageBox.Show("The value 3 is not in Departments".")
}
这篇关于将值与数组值进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文