【问题标题】:rearranging List<T> using lambda使用 lambda 重新排列 List<T>
【发布时间】:2011-09-08 16:40:59
【问题描述】:

我需要重新排列项目列表,以便所选项目位于列表的末尾,最后一个项目替换前一个项目,前一个项目替换它之前的项目,依此类推。

例如,如果我有一个包含 10 个项目的列表,并且所选项目位于第 5 位,则该项目位于第 9 位,并且 9 将替换 8,然后 8 替换 7,7 替换 6,并且 6 占据第 5 位。我管理使用此代码获得所需的结果:

List<int> numList = new List<int>();
int selectedNum = 5;//Selected at runtime
for (int i = 0; i < 10; i++) numList.Add(i);
int numListCount = numList.Count-1;
int tempNum = numList[numListCount];
List<int> tempList = numList.GetRange(selectedNum + 1,(numList.Count-selectedNum) - 2);
numList[numListCount] = selectedNum;
numList.RemoveRange(selectedNum, (numList.Count-selectedNum)-1);
numList.InsertRange(selectedNum, tempList);
numList.Insert(numListCount - 1, tempNum);

结果是:

0,1,2,3,4,6,7,8,9,5

我确定我的代码丑陋且效率低下:我有两个问题:

  1. 是否可以使用 Lambda 获得相同的结果?如果不是,那么
  2. 如何优化我的代码。谢谢。

【问题讨论】:

    标签: c# list lambda


    【解决方案1】:

    您可以使用Remove Method 删除所选项目并使用Add Method 将其附加到末尾:

    List<int> numList = new List<int>();
    for (int i = 0; i < 10; i++) numList.Add(2 * i);
    
    int selectedNum = 6; // selected at runtime
    numList.Remove(selectedNum);
    numList.Add(selectedNum);
    

    之前:

    0 2 4 6 8 10 12 14 16 18
    

    移除后(6):

    0 2 4 8 10 12 14 16 18
    

    添加后(6):

    0 2 4 8 10 12 14 16 18 6
    

    如果要在选定索引处移动项目,可以使用RemoveAt Method 而不是Remove Method

    List<int> numList = new List<int>();
    for (int i = 0; i < 10; i++) numList.Add(2 * i);
    
    int selectedIndex = 5; // selected at runtime
    int selectedNum = numList[selectedIndex];
    numList.RemoveAt(selectedIndex);
    numList.Add(selectedNum);
    

    之前:

    0 2 4 6 8 10 12 14 16 18
    

    RemoveAt(5) 之后:

    0 2 4 6 8 12 14 16 18
    

    添加后(10):

    0 2 4 6 8 12 14 16 18 10
    

    使用 LINQ,您将创建一个新列表,其中删除并附加了所选项目。不过,如上所示的就地更新效率更高。

    List<int> numList = new List<int>();
    for (int i = 0; i < 10; i++) numList.Add(2 * i);
    
    int selectedIndex = 5; // selected at runtime
    List<int> newNumList = numList.Take(selectedIndex)
                                  .Concat(numList.Skip(selectedIndex + 1))
                                  .Concat(numList.Skip(selectedIndex).Take(1))
                                  .ToList();
    

    numList:

    0 2 4 6 8 10 12 14 16 18
    

    newNumList:

    0 2 4 6 8 12 14 16 18 10
    

    【讨论】:

      【解决方案2】:

      据我了解,您只是想将给定项目移动到列表的底部,对吗?

      List<int> list = new List<int>();
      for (int i = 0; i < 10; i++) 
          numList.Add(i);
      int temp = list[5];
      list.RemoveAt(5);
      list.Add(temp);
      

      编辑:我的理解是您知道要移动的项目的位置 (5)。如果您知道该值,那么发布的另一个答案就是正确的答案

      【讨论】:

        【解决方案3】:

        您不需要所有额外的东西,包括临时列表。你可以这样做

        numList.Remove(selectedNum);
        numList.Add(selectedNum);
        

        就这么简单。

        【讨论】:

          【解决方案4】:

          现在无法检查,但这应该可以解决问题:

          var temp = list.Take(index-1).Concat(list.Skip(index)).Concat(list[index]);
          list = temp;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-09-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多