ArrayList是最老的容器了吧,C#1时代的,List<T>泛型容器是C#2之后的。那么请问,有没有什么情形,仍然推荐使用ArrayList而不是List<T>的? 效率问题?

解决方案 »

  1.   

    需要考虑这点效率的时候 直接抛弃.net库 去玩c或者汇编吧
      

  2.   

    List<object>
    和 ArrayList用法上基本无差别具体性能有无差异,这个没测试过
      

  3.   

    当数据量小的时候,ArrayList的操作时间上要比List<T>省。
      

  4.   

    微软官方的观点就是推出List<T>替代ArrayList
      

  5.   


    胡扯。
    List<T>在创建的时候的时间消耗比ArrayList要大?
      

  6.   

    为谨慎起见,我实际测试了下。using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                List<object>[] a1 = new List<object>[10000000];
                for (int i = 0; i < 10000000; i++)
                {
                    a1[i] = new List<object>();
                }
                sw.Stop();
                Console.WriteLine(sw.ElapsedTicks);
                sw.Reset();
                sw.Start();
                ArrayList[] a2 = new ArrayList[10000000];
                for (int i = 0; i < 10000000; i++)
                {
                    a2[i] = new ArrayList();
                }
                sw.Stop();
                Console.WriteLine(sw.ElapsedTicks);
            }
        }
    }90071119
    91062186
    Press any key to continue . . .运行了10多次,互有胜负。
      

  7.   

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                for (int i = 0; i < 1000000; i++)
                {
                    var x = new List<object>();
                    for (int j = 0; j < 100; j++)
                    {
                        x.Add(j);
                    }
                }
                sw.Stop();
                Console.WriteLine(sw.ElapsedTicks);
                sw.Reset();
                sw.Start();
                for (int i = 0; i < 1000000; i++)
                {
                    var y = new ArrayList();
                    for (int j = 0; j < 100; j++)
                    {
                        y.Add(j);
                    }
                }
                sw.Stop();
                Console.WriteLine(sw.ElapsedTicks);
            }
        }
    }
    测试100个元素的插入,算小数据量了吧。同样在一个数量级上。94096078
    107058149
    Press any key to continue . . .
      

  8.   

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                for (int i = 0; i < 1000000; i++)
                {
                    var x = new List<object>();
                    for (int j = 0; j < 10; j++)
                    {
                        x.Add(j);
                    }
                }
                sw.Stop();
                Console.WriteLine(sw.ElapsedTicks);
                sw.Reset();
                sw.Start();
                for (int i = 0; i < 1000000; i++)
                {
                    var y = new ArrayList();
                    for (int j = 0; j < 10; j++)
                    {
                        y.Add(j);
                    }
                }
                sw.Stop();
                Console.WriteLine(sw.ElapsedTicks);
            }
        }
    }17402617
    17170626
    Press any key to continue . . .
      

  9.   

    List不用装箱拆箱 把上面的例子改成List<int> 试试