zoukankan      html  css  js  c++  java
  • c#删除list中的元素

            public static void TestRemove() {
                string[] str = { "1", "2", "d", "x" };
                List<string> list = new List<string>(str);
    
                #region has error
                for (int i = 0; i < list.Count; i++)
                {
                    Console.WriteLine(list[i]);
                    Console.WriteLine("Result:" + i + ":" + i / 2);
                    if (i % 2 == 0)
                    {
                        list.Remove(list[i]);
                    }
                } 
                #endregion
    
                Console.WriteLine("============================================");
                list = new List<string>(str);
                for (int i = list.Count-1; i >= 0; i--)
                {
                    Console.WriteLine(list[i]);
                    Console.WriteLine("Result:" + i + ":" + i / 2);
                    if (i % 2 == 0)
                    {
                        Console.WriteLine(string.Format(" delete .index:{0} value:{1}",i,list[i]));
                        list.Remove(list[i]);
     
                    }
                }
            }
    output:

    1
    Result:0:0
    d
    Result:1:0
    x
    Result:2:1
    ============================================
    x
    Result:3:1
    d
    Result:2:1
    delete .index:2 value:d
    2
    Result:1:0
    1
    Result:0:0
    delete .index:0 value:1

     
     
  • 相关阅读:
    第03组 Alpha冲刺(3/6)
    第03组 Alpha冲刺(2/6)
    第03组 Alpha冲刺(1/6)
    团队项目-选题报告
    第3组 团队展示
    福大软工 · BETA 版冲刺前准备(团队)
    Alpha 事后诸葛亮
    Alpha冲刺
    Alpha冲刺-(9/10)
    Alpha冲刺
  • 原文地址:https://www.cnblogs.com/softidea/p/3345739.html
Copyright © 2011-2022 走看看