美文网首页
ArrayList与泛型类

ArrayList与泛型类

作者: 本来想取long但是有人用了 | 来源:发表于2016-12-15 19:53 被阅读0次

    多线程的使用方法之一

    using System.Threading;

    Thread th = new Thread (mc.Test4);

    th.Start ();

    Thread th2 = new Thread (mc.Test5);

    th2.Start ();

    集合:ArrayList属于非泛型集合

      ArrayList是一个数组集合.

      List

    它属于动态改变长度的数组集合,不需要我们管理.

    /// 

    ///非泛型集合添加元素演示

    /// 

    public void Test()

    {

    ArrayList al = new ArrayList ();

    //演示添加非对象元素

    al.Add ("刘德华");

    al.Add ("张学友");

    al.Add (1);

    //遍历数组

    for (int i = 0; i < al.Count; i++) {

    Console.WriteLine (al[i]);

    }

    foreach (var item in al) {

    Console.WriteLine (item);

    }

    }

    /// 

    ///泛型集合添加元素演示

    /// 

    public void Test2()

    {

    //这个T是占位符,不代表任何意义

    List list=new List();

    list.Add ("刘德华");

    list.Add ("张学友");

    }

    /// 

    ///演示非泛型集合添加10000000个元素所耗费的时间

    /// 

    public void Test4()

    {

    ArrayList a1 = new ArrayList ();

    Stopwatch sw = new Stopwatch ();

    sw.Start ();

    for (int i = 0; i < 10000000; i++) {

    a1.Add (i);

    }

    sw.Stop ();

    Console.WriteLine ("非泛型集合ArrayList添加100万元素所耗费的时间{0}",sw.Elapsed);

    }

    /// 

    ///演示泛型集合添加10000000个元素所耗费的时间

    /// 

    public void Test5()

    {

    Stopwatch sw = new Stopwatch ();

    sw.Start ();

    List l1 = new List ();

    for (int i = 0; i < 10000000; i++) {

    l1.Add (i);

    }

    sw.Stop ();

    Console.WriteLine ("泛型集合ArrayList添加100万元素所耗费的时间{0}",sw.Elapsed);

    }

    /// 

    ///非泛型集合排序/倒排与泛型基本相同

    /// 

    public void Test6()

    {

    //            ArrayList al = new ArrayList ();

    //            int[] arr = { 1, 2, 3, 4,3, 6, 7 };

    //            al.AddRange (arr);

    //

    //            //倒排

    //            al.Reverse ();

    //            //排序

    //            al.Sort ();

    //            foreach (var item in al) {

    //                Console.WriteLine (al);

    //            }

    List li=new List();

    HashSet hs=new HashSet();

    Hashtable ht = new Hashtable ();

    ht.Add (1, 3);

    ht.Add (2, 5);

    hs.Add (1);

    hs.Add (2);

    li.AddRange (hs);

    foreach (var item in li) {

    Console.WriteLine (item);

    }

    }

    /// 

    ///研究ArrayList的capacity

    /// 

    public void Test7()

    {

    ArrayList al = new ArrayList (3);

    Console.WriteLine (al.Capacity);

    al.Add (1);

    al.Add (2);

    al.Add (3);

    al.Add (3);

    Console.WriteLine (al.Capacity);

    }

    /// 

    ///研究ArrayList的insert()

    /// 

    /// The command-line arguments.

    public void Test8()

    {

    ArrayList a1 = new ArrayList ();

    int[] arr = { 1, 2, 3, 4,3, 6, 7 };

    a1.AddRange (arr);

    //插入指定的索引处

    a1.Insert (1,10);

    //移除第一个出现的3

    a1.Remove(3);

    //下标

    a1.RemoveAt(2);

    foreach (var item in a1) {

    Console.WriteLine (item);

    }

    }

    //T只是占位符,T只可以赋值,但是不能运算+ - * /

    //struct表示限定的是值类型

    //        public void Swap(ref T num1,ref T num2) where T:struct

    //        {

    //            T temp=default(T);

    //            temp = num1;

    //            num1 = num2;

    //            num2 = temp;

    //        }

    //演示引用类型限定

    //        public void Swap(ref T num1,ref T num2) where T:class

    //        {

    //            T temp=default(T);

    //            temp = num1;

    //            num1 = num2;

    //            num2 = temp;

    //        }

    public T Swap(ref T num1,ref T num2) where T:IComparable

    {

    if (num1.CompareTo (num2) < 0) {

    return num2;

    } else {

    return num1;

    }

    }

    //返回为0意味着相等

    //1表示降序

    //-1表示升序

    int a=10;

    int b = 9;

    int s=a.CompareTo (b);

    -\��{�\?j

    相关文章

      网友评论

          本文标题:ArrayList与泛型类

          本文链接:https://www.haomeiwen.com/subject/egpgmttx.html