简体   繁体   English

检查数组中的特定值

[英]Check specific values in an array

I have 2 arrays which I compare below. 我有2个数组,下面进行比较。

int[] values = { 1, 2, 3, 4, etc };

int[,] limits = { { 1, 2 }, { 2, 5 }, { 2, 6 },etc };

I can compare all the elements of the array quite easily, or specific elements in the array if they are sequential to see if they all fall within the corresponding limits using the following code, 我可以很容易地比较数组中的所有元素,或者使用以下代码比较数组中的特定元素(如果它们是顺序的),以查看它们是否都在相应的限制内,

 if (Enumerable.Range(0, values.Length).All(x => values[x] >= limits[x, 0] && values[x] <= limits[x, 1]))
 {
    //Do something            
 };

However, If I wanted to check only specific, non sequentional indecies in the array, eg indexes 0,4,6 & 9 how can I do this ? 但是,如果我只想检查数组中特定的,非顺序的索引,例如索引0、4、6和9,该怎么做? Thank in advance. 预先感谢。

只需将Enumerable.Range替换为要检查的索引的集合即可

new[] {0, 4, 6, 9}.All(x => values[x] >= limits[x, 0] && values[x] <= limits[x, 1])

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

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