C#手寫簡單的的ArrayList

1、定義自己的IMyList接口

namespace DataStructure
{
    public interface IMyList
    {
        object this[int index] {  get;  set; }
        void Add(object value);
        bool Contains(object value);
        void Clear();
        int IndexOf(object value);
        void Insert(int index, object value);
        bool Remove(object value);
        void RemoveAt(int index);
        int Count { get; }
    }
}

2、定義自己的MyArrayList實現IMyList接口

using System;
 
namespace DataStructure
{
    public class MyArrayList:IMyList
    {
        private readonly object[] _items;
 
        private readonly int _maxLength;
        
        public int Count { get; private set; }
        
        public object this[int index]
        {
            get => _items[index];
            set => _items[index] = value;
        }
 
        public MyArrayList(int maxLength)
        {
            Count = 0;
            _items=new object[maxLength];
            _maxLength = maxLength;
        }
 
        public void Add(object value)
        {
            if (Count+1 > _maxLength)
            {
                throw new Exception("超過定義的最大長度了");
            }
            _items[Count] = value;
            Count++;
        }
        
        public bool Contains(object value)
        {
            for (var i = 0; i < Count; i++)
            {
                if (value .Equals(_items[i]))
                {
                    return true;
                }
            }
            return false;
        }
 
        public void Clear()
        {
            Count = 0;
        }
 
        public int IndexOf(object value)
        {
            for (var i = 0; i < Count; i++)
            {
                if (value .Equals(_items[i]))
                {
                    return i;
                }
            }
            return -1;
        }
        
        public void Insert(int index, object value)
        {
            if (Count+1 > _maxLength)
            {
                throw new Exception("超過定義的最大長度了");
            }
 
            if (index < 0)
            {
                throw new Exception("index不能爲負數");
            }
            
            for (var i = Count-1; i >= index; i--)
            {
                _items[i + 1] = _items[i];
 
            }
            _items[index] = value;
            Count++;
        }
 
        //返回true說明List中包含了value值,返回false說明List中沒有value值
        public bool Remove(object value)
        {
            var index=IndexOf(value);
            if (index <= -1) return false;
            RemoveAt(index);
            return true;
        }
 
        public void RemoveAt(int index)
        {
            if (index < 0)
            {
                throw new Exception("index不能爲負數");
            }
 
            if (index >= Count)
            {
                throw new Exception("index超過了當前數組的最大角標了");
            }
            
            for (var i = index; i < Count; i++)
            {
                _items[i]= _items[i + 1];
 
            }
            Count--;
        }
 
        public override string ToString()
        {
            var str="";
            for (var i = 0; i < Count; i++)
            {
                str += _items[i].ToString()+" ";
            }
            return str;
        }
    }
}

注意:ArrayList和List十分類似。核心差異在ArrayList存儲的是object數組。List中存儲的是泛型數組(源碼中內部數組分別是object[] _items,T[] _items)。ArrayList雖然可以存儲不同類型,但是應用過程中需要拆箱與裝箱,同時類型也不如List安全。在應用環境中一般能用List就用List。

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