泛型和非泛型
来源:互联网 发布:千手观音知乎 编辑:程序博客网 时间:2024/06/11 19:48
原帖: http://blog.csdn.net/chpdirector84/article/details/4467424
泛型和非泛型
一:区别
1 : 非泛型类容器来说,该容器的元素类型是 object ,使用方便,无须指定具体的元素类型。但是这种使用上的方便带来了惨重的性能上的损失,因为,我们每次插入一个元素就要经过一次 “ 装箱 ” 操作,每次访问一个元素就要经过一次 “ 拆箱 ” 操作。
ArrayList , Hashtable , Queue , Stack , SortedList , CollectionBase , ReadOnlyCollectionBase 。
2 : “ 泛型 ” 容器就不一样了,这种类型的容器和 C++ 里的容器 ( 比如 vector 、 map 、 list 等 ) 一样,在使用的时候都要指定元素类型,并且在 “ 编译阶段 ” 完成类型制定,而且也避免了 “ 装箱、拆箱、类型转换 ” 等无谓的性能损失。
List , Dictonary , Queue , Stack , SortedList , Collection , ReadOnlyCollection 。
二:如何选择集合类
1 : FIFO : Queue 。 LIFO : Stack 。
2 :按索引访问: ArrayList , List 。
3 :按索引键访问: Hashtable , SortedList , Dictonary 。 Hashtable 对于大量数据如几万,几十万时,数据搜索效率高。 SortedList 一般用在几百,几千条数据时用。
三:SortedList键值允许重复
struct SortedListValue
{
public SortedListValue(int a, int b)
{
this.a = a;
this.b = b;
}
public int a;
public int b;
}
class MyComparer : IComparer
{
#region IComparer Members
//希望键值重复的话,就不能返回0
public int Compare(object x, object y)
{
int a = (int)x;
int b = (int)y;
if (a.Equals(b))
{
return -1;
}
return a.CompareTo(b);
}
#endregion
}
class Program
{
static void Main(string[] args)
{
SortedList sl = new SortedList(new MyComparer());
sl.Add(1, new SortedListValue(1, 2));
sl.Add(2, new SortedListValue(3, 2));
sl.Add(3, new SortedListValue(2, 2));
sl.Add(1, new SortedListValue(4, 2));
sl.Add(1, new SortedListValue(5, 2));
sl.Add(2, new SortedListValue(1, 2));
DisplayResult(sl);
}
static void DisplayResult(SortedList sl)
{
foreach (DictionaryEntry de in sl)
{
Console.WriteLine("键:{0},键值为:{1}", de.Key, ((SortedListValue)(de.Value)).a);
}
}
}
}
- 泛型和非泛型
- 泛型和非泛型
- 泛型集合和非泛型集合
- 泛型和反射;getClass()和.class
- 内联和块级和泛型
- 反射和泛型
- 集合和泛型
- 模板和泛型
- STL和泛型
- 泛型和反射
- 泛型和数组
- 集合和泛型
- 集合和泛型
- 泛型和反射
- 泛型和反射
- 泛型和注解
- 反射和泛型
- 泛型和反射
- Linux shell位置参数
- 块设备--blk.h/hd.c/ll_rw_blk.c源码分析
- 剑指offer之两个链表的第一个公共节点
- (ACM学习笔记)POJ 1160中四边形不等式的应用
- 关于Spring中的<context:annotation-config/>配置
- 泛型和非泛型
- C++中四种类型转换运算符的使用方法
- AIX查看CPU及内存参数
- Matrix相关
- SVN 解决Unable to connect to a repository at URL 禁止访问 (forbidden)
- caffe, torch, theano
- 在Linux下的命令行以及eclipse运行Hadoop提供的Wordcount例子
- jquery解决版本冲突
- 【积少成多】各种js或者jquery或者html或者css等微知识------持续更新!