List traverse delete from scratch babbling

Bearing in mind the performance efficiency often write code, so every time I write from the traversal, it is clear that you wrote

        static void Main(string[] args)
        {
            List<int> a = new List<int>();
            a.Add(1);
            a.Add(2);
            a.Add(3);
            a.Add(4);
            a.Add(5);
            a.Add(6);

            int count = a.Count;
            for (int i = 0; i < count; i++)
            {
                if (a[i]==3)
                {
                    a.Remove(a[i]);
                 
                }
                Console.WriteLine(a[i]);
            }
            Console.ReadLine();
        }

This traversal, count once considered good, when you do not need to traverse each calculation. But more than write code is wrong.

Delete list, it will automatically change the length of the count, but the count is fixed, it will error.

and so. . . He recalled the first time the state had to write code. Program perfect pass.

   static void Main(string[] args)
        {
            List<int> a = new List<int>();
            a.Add(1);
            a.Add(2);
            a.Add(3);
            a.Add(4);
            a.Add(5);
            a.Add(6);


            for (int i = 0; i < a.Count; i++)
            {
                if (a[i]==3)
                {
                    a.Remove(a[i]);
                 
                }
                Console.WriteLine(a[i]);
            }
            Console.ReadLine();
        }

 

Guess you like

Origin www.cnblogs.com/big-zhou/p/11612884.html