C# 如何列出。删除

C# 如何列出。删除,c#,optimization,C#,Optimization,我有一个UDT,比如说: struct myudt{ public string key; //unique for each item public ... // whatever } 和一份清单: List<myudt> myList; 有更多的方法来实现这一点吗 谢谢你的帮助。 您的Remove1方法需要O(n)在IndexOf中查找的时间和O(n)执行删除的时间(因为内部T[]数组中的每个元素都需要重新定位) 您的Remove2方法需要O(n)时间在Fi

我有一个UDT,比如说:

struct myudt{
    public string key; //unique for each item
    public ... // whatever
}
和一份清单:

List<myudt> myList;
有更多的方法来实现这一点吗

谢谢你的帮助。

  • 您的
    Remove1
    方法需要
    O(n)
    IndexOf
    中查找的时间和
    O(n)
    执行删除的时间(因为内部
    T[]
    数组中的每个元素都需要重新定位)

  • 您的
    Remove2
    方法需要
    O(n)
    时间在
    Find
    中进行查找,然后在
    Remove
    中进行另一次
    O(n)
    时间来重新定位内部数组元素

  • 因此,在一般情况下,
    Remove1
    Remove2
    快约50%(
    3n
    vs
    2n

  • 此外,由于您在程序中使用的是
    struct
    ,因此即使需要使用
    List.Remove,也会比需要的速度慢


然而,这两种方法仍然是次优的。如果您需要存储任意项集合,以便添加和删除元素,并且元素没有自然或固有的顺序以利于任何其他计算,则应根据您的应用程序使用
哈希集
字典

由于您的
myudt
类型有一个键值,我认为使用
Dictionary
是合适的-但是您需要确保您的
key
属性是不可变的-并且您应该使用
class
而不是
struct

class MyType {
    public MyType( String key ) {
        this.Key = key ?? throw new ArgumentNullException(nameof(key));
    }
    public String Key { get; } // <-- This is an immutable property
}

static class DictionaryExtensions {

    public static void Add( this Dictionary<String,MyType> dict, MyType t )
    {
        dict.Add( t.Key, t );
    }

    public static void Remove( this Dictionary<String,MyType> dict, MyType t )
    {
        dict.Remove( t.Key );
    }
}

// This program below will run lightning fast as all individual operations have O(1) time complexity:

Dictionary<String,MyType> dict = new Dictionary<String,MyType>();

for( Int32 i = 0; i < 10000; i++ ) {
    
    MyType t = new MyType( key: i.ToString() );
    dict.Add( t );
}

for( Int32 i = 0; i < 10000; i++ ) {
    
    String key = i.ToString();
    dict.Remove( key );
}

类MyType{
公共MyType(字符串键){
this.Key=Key??抛出新的ArgumentNullException(nameof(Key));
}

public String Key{get;}//为什么首先要从
列表
中删除项?你熟悉计算机科学的基本数据结构吗?如果熟悉,那么你就会明白为什么
列表
对于存储需要随机(任意)的可变数据是一个非常糟糕的选择元素删除。@meysamasadi是的,这很重要!-算法时间复杂性是一个程序花一年时间做某事与花半毫秒之间的差异。
List.RemoveAt(Int32)
具有
O(n)
复杂性,而
Remove(T)
至少需要
2n
(虽然这仍然是按<代码> o(n)< /> >的顺序,但这是一个明显的差别)。我建议您在引用OOP应用程序代码时(尤其是在爪哇、C、C++等)中避免使用“UDT”这个词,因为不是库类型的所有东西都是UDT。通常只为数据库系统中的用户定义类型保留。在数据库系统中,用户定义类型相对较少。此外,您应该阅读以下内容:@Dai:您的建议是不要使用List和nor-udt。但是您会使用什么来代替呢?如果您的类型只有两个或三个原语,并且没有代码或事件,那么即使是一个类也会被过度杀死。使用当移除中间元素时,数组也会很好。请理解任何建设性建议。
class MyType {
    public MyType( String key ) {
        this.Key = key ?? throw new ArgumentNullException(nameof(key));
    }
    public String Key { get; } // <-- This is an immutable property
}

static class DictionaryExtensions {

    public static void Add( this Dictionary<String,MyType> dict, MyType t )
    {
        dict.Add( t.Key, t );
    }

    public static void Remove( this Dictionary<String,MyType> dict, MyType t )
    {
        dict.Remove( t.Key );
    }
}

// This program below will run lightning fast as all individual operations have O(1) time complexity:

Dictionary<String,MyType> dict = new Dictionary<String,MyType>();

for( Int32 i = 0; i < 10000; i++ ) {
    
    MyType t = new MyType( key: i.ToString() );
    dict.Add( t );
}

for( Int32 i = 0; i < 10000; i++ ) {
    
    String key = i.ToString();
    dict.Remove( key );
}