【问题标题】:Exchange bool values between two Lists, C#在两个列表之间交换布尔值,C#
【发布时间】:2017-06-09 14:02:21
【问题描述】:

请帮助我找到一种方法来执行以下操作:

我有两个List<bool>

第一个包含不同的布尔值,我对第二个的长度和内容一无所知。我必须通过第一个List<bool>,如果我遇到一个真实值,我需要将它写在第二个List<bool> 中,与我在第一个List<bool> 中遇到它的位置完全相同。

例如,第一个列表由 false-false-true 组成,这意味着我应该在第二个 List<bool> 的第三个位置写 true,第二个应该看起来像 ...-...-true 并且如果在 ... 位置上有一些东西我应该保留它,如果那里什么都没有,我应该把 false 放在那里。

【问题讨论】:

  • 有没有尝试自己解决?
  • 你如何解释你的问题非常令人困惑......请在你想做的事情中添加更多细节。也不太可能有一个 List 来解决问题。
  • 对于第一个列表包含false 值的地方,您期望在第二个列表中是什么?
  • "我对长度一无所知"你需要注意第二个列表比第一个短的情况,否则你会遇到麻烦
  • @MonZhu OP 指定如果没有,则必须插入 false。

标签: c# list collections iteration


【解决方案1】:

据我所知,唯一的(小)困难在于:Math.Min(list1.Count, list2.Count):

List<bool> list1 = new List<bool>() { true, false, true, false };
List<bool> list2 = new List<bool>() { false };

...

// Math.Min(list1.Count, list2.Count): ...I know nothing about the length
for (int i = 0; i < Math.Min(list1.Count, list2.Count); ++i)
  if (list1[i])      // ... if I meet a true value
    list2[i] = true; // I need to write it in the second ... at the exactly same position

测试:

 // [true]
 Console.Write("[" + string.Join(", ", list2) + "]");

编辑:如果您想填充list2false 以确保所有true 都在它们的索引中:

 for (int i = 0; i < list1.Count; ++i)
    if (list1[i]) {
      if (i >= list2.Count) // do we want padding?
        list2.AddRange(new bool[i - list2.Count + 1]);

      list2[i] = true;
    }

测试:

 // [true, false, true]
 Console.Write("[" + string.Join(", ", list2) + "]");

【讨论】:

  • 添加大括号,你的代码质量会失败:))
【解决方案2】:
var firstList = new List<bool> { false, false, true };
var secondList = new List<bool> { true };

for(int i =0; i < firstList.Count; i++)
{
    var currentItem = firstList[i];

    // If the item in the first list is true, then we need to insert it into the second list
    if(currentItem)
    {
        // If the second list is shorter than the actual index,
        // we insert missing elements so we can be able to insert the matching true in first list 
        // at the exact same position in second list.
        while(secondList.Count < i)
        {
            // Inserting false as you ask
            secondList.Add(false);
        }

        // Now the second list has at at least enough elements, 
        // we can insert true at the exact same location. 
        // Note that this will shift all other elements after that index
        secondList.Insert(i, currentItem);
    }
}

结果:

foreach(var item in secondList)
{
    Console.WriteLine(item);    
}

// true    <- already in the second list at the begining
// false   <- added by default because there was no element in 2nd position
// true    <- added because in the first list

注意,如果要替换相同位置的现有值,则需要使用此代码(注意 while 条件中的 i+1 和将 setter indexer 与 secondList 一起使用:

while(secondList.Count < i+1)
{
    secondList.Add(false);
}

secondList[i] = currentItem;

【讨论】:

    猜你喜欢
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 2022-07-26
    相关资源
    最近更新 更多