創建自定義ArrayList步驟

1、          先初始化一個數組

object [] objarr=new object [0];

2、          仿照ArrayList見三個構造函數

public MyArrayList()

        {

        }

        public MyArrayList(int Cap)//容量

        {

          objarr =new object [Cap ];

        }

        public MyArrayList(ICollection con)//定義非泛型集合大小

        {

          objarr =new object [con.Count ];

          con.CopyTo(objarr, 0);

       }

3、         創建索引器

public object this[int i]//This指實例化的對象,[int i]

            {                                數組下表

            get

            {

                return objarr[i];

            }

          

            set

            {

                objarr[i] = value;

            }

 }

4、         寫方法,例如添加移除等

public int   Add(object o)

        {

 object[] tem = new object[objarr.Length + 1];

            objarr.CopyTo(tem, 0);

            tem[tem.Length - 1] = o;

            objarr = tem;

            return tem.Length -1;

 

        }    

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章