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

     
     
  • 相关阅读:
    jquery web 國際化
    Struts2 分割字符串标签s:generator
    (55) 销售锁货功能
    (54) 记录销售单修改详细
    (53) 动态列表自定义
    exe文件作为服务启动
    (52)KeyError错误
    (51) magento集成增加到款通知
    (50)与magento集成
    (49) odoo context操作
  • 原文地址:https://www.cnblogs.com/softidea/p/3345739.html
Copyright © 2011-2022 走看看