[英]Searching specific indexes in arrays for specific numbers
Hi there is there a way to check specific integer array indexes for specific numbers in C#; 嗨,有一种方法可以检查C#中特定数字的特定整数数组索引; for example what I would love to have worked would be: 例如,我希望工作的将是:
if(si[6] || si[7] || si[8] == 3)
{
MessageBox.Show("3 detected")
}
else
{
continue();
{
Obviously this doesn't work. 显然这不起作用。 Is there a clean way to do this? 有干净的方法吗? Thank you for looking. 谢谢你的期待。
var indexes = new int[] {6, 7, 8};
if (indexes.Any(i => si[i] == 3))
{
MessageBox.Show("3 detected")
}
最简单的是进行三次单独检查:
if (si[6] == 3 || si[7] == 3 || si[8] == 3)
You could do this a bit neater using a method with a params: 你可以使用带参数的方法做一点整洁:
public static bool HasValue(int value, params int[] itemsToCheck)
{
bool valueDetected = false;
foreach(var item in itemsToCheck)
{
valueDetected |= item == value;
}
return valueDetected;
}
Then you could just call it like this: 然后你可以像这样调用它:
if (HasValue(3, si[6], si[7], si[8]))
{
}
You can use Array.IndexOf function to find the index of the integer. 您可以使用Array.IndexOf函数来查找整数的索引。 If array has the integer then it will return the index else it will return -1. 如果数组有整数,那么它将返回索引,否则它将返回-1。
Like this int[] a = new int[] { 1, 2 }; 像这个int [] a = new int [] {1,2}; int c = Array.IndexOf(a, 2); int c = Array.IndexOf(a,2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.