c# - 在 boolean 数组中查找索引从false更改为true?

标签 c# .net arrays linq boolean

我有两个数组:

bool[] oldValues = GetCurrentValuesFromSomewhere ();

ChangeCurrentValues ();

bool[] newValues = GetCurrentValuesFromSomewhere ();

List<int> whichIndexsHasBeenChangedFromFalseToTrue = /* linq */


任何的想法?除了列表以外,它也可以是bool[]数组。

最佳答案

您可以使用类似以下的内容:

var changedValues =
    (from i in Enumerable.Range(0, oldValues.Length)
     where !oldValues[i] && newValues[i]
     select i)
    .ToList();


或者,如果您更喜欢流利的语法:

var changedValues = Enumerable
     .Range(0, oldValues.Length)
     .Where(i => !oldValues[i] && newValues[i])
     .ToList();


如果需要bool[]结果,可以使用以下方法:

var changedValues =
    (from i in Enumerable.Range(0, oldValues.Length)
     select !oldValues[i] && newValues[i])
    .ToArray();


或使用流利的语法:

var changedValues = Enumerable
     .Range(0, oldValues.Length)
     .Select(i => !oldValues[i] && newValues[i])
     .ToArray();

关于c# - 在 boolean 数组中查找索引从false更改为true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20005377/

相关文章:

c# - 字典和 SelectMany()

c# - 使用 pfx 文件加载证书

c# - 使用 IEnumerator 和使用数组作为属性之间的区别

c# - 可选属性默认值的 XML 序列化

c# - 我想在数组中获取索引,其中包含我在 C# 中的值

java - System.arraycopy是否使用clone()方法?

c - C 中的免费多维字符** 不起作用

c# - 渲染 View 时 foreach 出现奇怪的空引用错误

c# - Windows 应用商店应用程序中的大字节数组

c# - 如何在 .net 核心中散列密码,使其在 .net 框架中相等