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

     
     
  • 相关阅读:
    leetcode--95. Unique Binary Search Trees II
    leetcode--96. Unique Binary Search Trees
    leetcode分类总结
    leetcode-115. Distinct Subsequences
    tcpdump安装配置及抓包分析
    dp经典案例
    tree的各种问题
    大数的阶乘算法
    由mmap引发的SIGBUS
    win10系统下如何查看端口被哪个进程占用
  • 原文地址:https://www.cnblogs.com/softidea/p/3345739.html
Copyright © 2011-2022 走看看