ArrayList简单实现代码,欢迎大家指点

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace MyArrayList_hjf
{  
    class MyArrayList : IEnumerable
    {
        object[] objarr = new object[0];
        int count;
        public MyArrayList()
        {

        }
        public MyArrayList(int capacity)
        {
            this.Capacity = capacity;
        }
        /// <summary>
        /// 索引器
        /// </summary>
        /// <param name="index">索引值</param>
        /// <returns></returns>
        public object this[int index]
        {
            get
            {
                if (index > count)
                {
                    throw new Exception("索引超出范围");
                }
                else
                {
                    if (index > count)
                    {
                        throw new Exception("索引超出范围");
                    }
                    else
                    {
                        return objarr[index];
                    }
                }
            }
            set
            {
                if (index < count)
                {
                    objarr[index] = value;
                }
                else
                {
                    throw new Exception("索引超出范围");
                }
            }
        }
        /// <summary>
        /// 获取数组的长度
        /// </summary>
        public int Count
        {
            get
            {
                return count;
            }
        }
        /// <summary>
        /// 获取或设置数组的容量
        /// </summary>
        public int Capacity
        {
            get
            {
                return objarr.Length;
            }
            set
            {
                if (value < count)
                {
                    throw new Exception("容量小于元素个数");
                }
                else
                {
                    object[] temparr = new object[value];
                    for (int i = 0; i < count; i++)
                    {
                        temparr[i] = objarr[i];
                    }
                    objarr = temparr;
                }
            }
        }
        /// <summary>
        /// 添加数组元素
        /// </summary>
        /// <param name="value">要添加的值</param>
        /// <returns></returns>
        public int Add(object value)
        {
            if (objarr.Length == 0)
            {
                objarr = new object[4];
                objarr[0] = value;
                count++;
                return 0;
            }
            else
            {
                if (Count < objarr.Length)
                {
                    objarr[count] = value;
                    count++;
                    return count - 1;
                }
                else
                {
                    object[] tempObj = new object[objarr.Length * 2];
                    for (int i = 0; i < count; i++)
                    {
                        tempObj[i] = objarr[i];
                    }
                    tempObj[count] = value;
                    count++;
                    objarr = tempObj;
                    return count - 1;
                }
            }
        }
        /// <summary>
        /// 移除指定项的第一个匹配元素
        /// </summary>
        /// <param name="obj">要移除的对象</param>
        public void Remove(object obj)
        {
            int num = -1;
            for (int i = 0; i < count; i++)
            {
                if (objarr[i].Equals(obj))
                {
                    num = i;
                    break;
                }
            }
            if (num != -1)
            {
                for (int j = num; j < count; j++)
                {
                    objarr[j] = objarr[j + 1];
                }
                count--;
            }
        }
        /// <summary>
        /// 用于foreach循环
        /// </summary>
        /// <returns></returns>
        public IEnumerator GetEnumerator()
        {
            for (int i = 0; i < count; i++)
            {
                yield return objarr[i];
            }
        }
        /// <summary>
        /// 移除数组中所有元素
        /// </summary>
        public void Clear()
        {
            for (int i = 0; i < count; i++)
            {
                objarr[i] = null;
            }
            count = 0;
        }
        /// <summary>
        /// 把数组中所有元素反转
        /// </summary>
        public void Reverse()
        {           
            object[] tempArr = new object[count];
            int num = 0;
            for (int i = count - 1; i >= 0; i--)
            {
                tempArr[num] = objarr[i];
                num++;
            }
            objarr = tempArr;
        }
        /// <summary>
        /// 确定某元素是否存在与该数组中
        /// </summary>
        /// <param name="item">要在数组中查找的对象,该对象可以为null</param>
        /// <returns></returns>
        public bool Contains(object item)
        {
            bool mark = false;
            foreach (object o in objarr)
            {
                if (o.Equals(item))
                {
                    mark = true;
                    break;
                }
            }
            return mark;
        }
        /// <summary>
        /// 移除指定索引处的元素
        /// </summary>
        /// <param name="index">index:要移除的元素的从0开始的索引</param>
        public void RemoveAt(int index)
        {
            if (index < 0 || index > count)
            {
                throw new Exception("索引超出范围,必须为非负值并小于集合大小。");
            }
            else
            {
                for (int i = index; i < count; i++)
                {
                    objarr[i] = objarr[i + 1];
                }
                count--;
            }
        }
       
        /// <summary>
        /// 将元素插入到指定的索引处
        /// </summary>
        /// <param name="index">index:从0开始的索引,应该在该位置插入value</param>
        /// <param name="value">要插入的值,该值可以为null</param>
        public void Insert(int index, object value)
        {
            ArrayList al = new ArrayList();           
            if (index < 0 || index > count)
            {
                throw new Exception("插入索引已超出范围,必须为非负值,并且小于或等于集合大小。");
            }
            else
            {
                //如果数组长度小于集合容量
                if (count < Capacity)
                {                   
                    if (index < count)
                    {
                        for (int i = count - 1; i >= index; i--)
                        {
                            objarr[i + 1] = objarr[i];
                        }
                        objarr[index] = value;
                        count++;
                    }
                    else if (index == count)
                    {
                        objarr[index] = value;
                        count++;
                    }
                }
                else if (count == Capacity)
                {
                    object[] tempArr = new object[objarr.Length * 2];
                    for (int j = 0; j < count;j++ )
                    {
                        tempArr[j]=objarr[j];
                    }
                    objarr = tempArr;
                    if (index < count)
                    {
                        for (int i = count - 1; i >= index; i--)
                        {
                            objarr[i + 1] = objarr[i];
                        }
                        objarr[index] = value;
                        count++;
                    }
                    else if (index == count)
                    {
                        objarr[index] = value;
                        count++;
                    }
                }
            }
        }
    }
}

posted @ 2011-07-20 19:37  贺俊峰  阅读(838)  评论(0编辑  收藏  举报