这种名单有名字吗? [英] Is there a name for this kind of List?

查看:73
本文介绍了这种名单有名字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Teacher
   {
       public string TeacherId { get; set; }
       public string TeacherName { get; set; }
       public string Subject { get; set; }

   }

   List<Teacher> dayList = new List<Teacher>();



另外,我曾经认为List只能是用于存储单个列表,如...

List.Add(2);

List.Add(20);



如果我们可以像我在顶部给出的例子一样使用它,那么List和Dictionary之间有什么区别?因为在示例中第一个字段是TeacherId,它可以用作键右?


Also, i used to think that List can only be used to store a single list like ..
List.Add(2);
List.Add(20);

If we can use it like the example i gave at the top then what is the difference between List and Dictionary? Because in the example the first field is TeacherId which can be used as a key right?

推荐答案

检查此链接

列表和字典之间的差异 [ ^ ]


列表< Teacher> 只是列表。它仍然与词典完全不同。



TeacherId 不是,与词典的方式相同。

是的,你可以扫描列表以查找具有特定 TeacherId 的条目,但这需要时间与集合中的项目数成比例( O(n) 的)。您还可以扫描列表中具有相同时间比例的教师对象的任何其他属性。



字典是一个集合,其中特定值用作在大约 常量时间。键值是 必然集合中项目的属性。

所以字典可以基于值有效地查找项目,但是当项目被插入时,项目和密钥的关联被建立到具有指定键值的字典中。如果键值发生是项目的属性,并且项目的属性已更改,则新值将成为该项目的关键。必须使用旧密钥从Dictionary中删除该项目,然后可以重新插入与新密钥相关联的项目。



请参阅:< a href =http://msdn.microsoft.com/en-us/library/xfhwa508.aspx> http://msdn.microsoft.com/en-us/library/xfhwa508.aspx [< a href =http://msdn.microsoft.com/en-us/library/xfhwa508.aspxtarget =_ blanktitle =新窗口> ^ ]
This List<Teacher> is just a List. It is still quite different from a Dictionary.

The TeacherId is not a key in the same way that it would be with a Dictionary.
Yes, you could scan the list to find the entry with a specific TeacherId, but that takes time proportional to the number of items in the collection, on average (O(n)). You could also scan the list for any of the other properties of the Teacher objects with the same time proportionality.

A Dictionary is a collection where a specific value is used as the key to find items in the collection in approximately constant time. The key value is not necessarily a property of the items in the collection.
So a Dictionary can efficiently find items based on a value, but the assocition of the item and the key is established when the item is inserted into the Dictionary with the specified key value. If the key value happens to be a property of the item, and that property of an item is changed, then the new value will not become the key to that item. The old-key would have to be used to remove the item from the Dictionary and then the item could be re-inserted associated with the new key.

See: http://msdn.microsoft.com/en-us/library/xfhwa508.aspx[^]


粗略地说,从用户的角度来看,列表是一个退化的字典,即一个字典,其整数
Roughly speaking, form user''s point of view, a List is a degenerate Dictionary, that is a Dictionary whose keys are integers.


这篇关于这种名单有名字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆