這里寫自定義目錄標題
- List
- DIctionary
List
MyList類:這是一個泛型類,能夠存儲任意類型的元素。
_items數組:用于實際存儲元素。
_size變量:記錄當前列表中的元素數量。
構造函數:初始化數組容量為 4。
Count屬性:獲取列表中的元素數量。
索引器this[int index]:用于訪問列表中的元素。
Add方法:向列表中添加元素,若數組容量不足,會調用EnsureCapacity方法來擴容。
EnsureCapacity方法:確保數組容量足夠,若不足則將數組容量擴大為原來的 2 倍。
using System;// 自定義列表類
public class MyList<T>
{private T[] _items;private int _size;// 構造函數,初始化數組容量public MyList(){_items = new T[4];_size = 0;}// 獲取列表中的元素數量public int Count{get { return _size; }}// 索引器,用于訪問列表中的元素public T this[int index]{get{if (index < 0 || index >= _size){throw new IndexOutOfRangeException();}return _items[index];}set{if (index < 0 || index >= _size){throw new IndexOutOfRangeException();}_items[index] = value;}}// 向列表中添加元素public void Add(T item){if (_size == _items.Length){EnsureCapacity(_size + 1);}_items[_size++] = item;}// 確保數組容量足夠private void EnsureCapacity(int min){if (_items.Length < min){int newCapacity = _items.Length == 0 ? 4 : _items.Length * 2;if (newCapacity < min){newCapacity = min;}Array.Resize(ref _items, newCapacity);}}
}class Program
{static void Main(){// 創建自定義列表實例MyList<int> myList = new MyList<int>();// 添加元素myList.Add(1);myList.Add(2);myList.Add(3);// 訪問元素for (int i = 0; i < myList.Count; i++){Console.WriteLine(myList[i]);}}
}
DIctionary
MyDictionary<TKey, TValue>類:這是一個泛型類,可存儲任意類型的鍵值對。
Entry結構體:用于存儲單個鍵值對,包含鍵的哈希碼、鍵和值。
_entries數組:實際存儲鍵值對的數組。
_count變量:記錄當前字典中的鍵值對數量。
構造函數:初始化數組容量為 InitialCapacity(這里設為 4)。
Count屬性:獲取字典中的鍵值對數量。
索引器this[TKey key]:用于根據鍵獲取或設置值。
TryGetValue方法:嘗試根據鍵獲取值,如果找到則返回 true 并將值賦給輸出參數 value,否則返回 false。
Add方法:向字典中添加鍵值對,如果鍵已存在則拋出異常。
Insert方法:插入鍵值對,會檢查鍵是否已存在,若已存在且 add 參數為 true 則拋出異常,否則更新值。
EnsureCapacity方法:確保數組容量足夠,若不足則將數組容量擴大為原來的 2 倍。
using System;// 自定義字典類
public class MyDictionary<TKey, TValue>
{private const int InitialCapacity = 4;private Entry[] _entries;private int _count;// 內部存儲的鍵值對結構private struct Entry{public int HashCode;public TKey Key;public TValue Value;}// 構造函數,初始化數組容量public MyDictionary(){_entries = new Entry[InitialCapacity];_count = 0;}// 獲取字典中的鍵值對數量public int Count{get { return _count; }}// 索引器,用于根據鍵獲取或設置值public TValue this[TKey key]{get{if (TryGetValue(key, out TValue value)){return value;}throw new KeyNotFoundException();}set{Insert(key, value, false);}}// 嘗試根據鍵獲取值public bool TryGetValue(TKey key, out TValue value){for (int i = 0; i < _count; i++){if (Equals(_entries[i].Key, key)){value = _entries[i].Value;return true;}}value = default(TValue);return false;}// 添加鍵值對public void Add(TKey key, TValue value){Insert(key, value, true);}// 插入鍵值對private void Insert(TKey key, TValue value, bool add){if (_count == _entries.Length){EnsureCapacity(_count + 1);}int hashCode = key.GetHashCode();for (int i = 0; i < _count; i++){if (_entries[i].HashCode == hashCode && Equals(_entries[i].Key, key)){if (add){throw new ArgumentException("An item with the same key has already been added.");}_entries[i].Value = value;return;}}_entries[_count].HashCode = hashCode;_entries[_count].Key = key;_entries[_count].Value = value;_count++;}// 確保數組容量足夠private void EnsureCapacity(int min){if (_entries.Length < min){int newCapacity = _entries.Length == 0 ? InitialCapacity : _entries.Length * 2;if (newCapacity < min){newCapacity = min;}Array.Resize(ref _entries, newCapacity);}}
}class Program
{static void Main(){// 創建自定義字典實例MyDictionary<string, int> myDictionary = new MyDictionary<string, int>();// 添加鍵值對myDictionary.Add("apple", 1);myDictionary.Add("banana", 2);myDictionary.Add("cherry", 3);// 根據鍵獲取值int value;if (myDictionary.TryGetValue("banana", out value)){Console.WriteLine($"The value of 'banana' is: {value}");}// 使用索引器設置值myDictionary["apple"] = 5;Console.WriteLine($"The new value of 'apple' is: {myDictionary["apple"]}");}
}