C#的List和DIctionary实现原理(手搓泛型类以及增删查改等功能)

这里写自定义目录标题

List

MyList类:这是一个泛型类,能够存储任意类型的元素。

_items数组:用于实际存储元素。

_size变量:记录当前列表中的元素数量。

构造函数:初始化数组容量为 4。

Count属性:获取列表中的元素数量。

索引器this[int index]:用于访问列表中的元素。

Add方法:向列表中添加元素,若数组容量不足,会调用EnsureCapacity方法来扩容。

EnsureCapacity方法:确保数组容量足够,若不足则将数组容量扩大为原来的 2 倍。

csharp 复制代码
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 倍。

csharp 复制代码
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"]}");
    }
}    
相关推荐
一然明月1 天前
C#语言基础详解和面向对象编程核心概念与高级特性详解(万字详解带示例代码)
开发语言·c#
宵时待雨1 天前
数据结构(初阶)笔记归纳4:单链表的实现
c语言·开发语言·数据结构·笔记·算法
WHOVENLY1 天前
揭秘正则表达式的基础语法与应用
开发语言·javascript·正则表达式
大雨淅淅1 天前
【开发工具】Postman从入门到精通:环境搭建与使用全攻略
开发语言·lua
flysh051 天前
.NET 基础 - StringBuilder 类
开发语言·c#·编程语言·c#10
甄心爱学习1 天前
Python 中 combinations 的详细用法
开发语言·python
独自归家的兔1 天前
Java性能优化实战:从基础调优到系统效率倍增 -2
java·开发语言·性能优化
独自归家的兔1 天前
Java性能优化实战:从基础调优到系统效率倍增 - 1
java·开发语言·性能优化
小π军1 天前
C++ STL:array容器常见用法
开发语言·c++
156082072191 天前
在QT下添加QWT6.1.4功能
开发语言·qt