benben 发表于 2013-2-4 23:48:15

C#的6种常用集合类大比拼【月儿原创】

C#的6种常用集合类大比拼

作者:清清月儿
主页:http://blog.csdn.net/21aspnet/           时间:2007.6.27 
说明:MSDN没有说出几种集合类其间的区别,当然欲知更多细节可参考MSDN。
一.先来说说数组的不足(也可以说集合与数组的区别):
1.数组是固定大小的,不能伸缩。虽然System.Array.Resize这个泛型方法可以重置数组大小,但是该方法是重新创建新设置大小的数组,用的是旧数组的元素初始化。随后以前的数组就废弃!而集合却是可变长的
2.数组要声明元素的类型,集合类的元素类型却是object.
3.数组可读可写不能声明只读数组。集合类可以提供ReadOnly方法以只读方式使用集合。
4.数组要有整数下标才能访问特定的元素,然而很多时候这样的下标并不是很有用。集合也是数据列表却不使用下标访问。很多时候集合有定制的下标类型,对于队列和栈根本就不支持下标访问!
 
二.下面讲述6种常用集合
1.ArrayList类
<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusing System;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusing System.Collections.Generic;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusing System.Text;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusing System.Collections;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifnamespace ConsoleApplication1
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    class Program
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        static void Main(string[] args)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif        ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            ArrayList al = new ArrayList();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            al.Add(100);//单个添加
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif            foreach (int number in new int[6] ...{ 9, 3, 7, 2, 4, 8 })
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif            ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                al.Add(number);//集体添加方法一//清清月儿 http://blog.csdn.net/21aspnet/
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif            }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif            int[] number2 = new int[2] ...{ 11,12 };
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            al.AddRange(number2);//集体添加方法二
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            al.Remove(3);//移除值为3的
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            al.RemoveAt(3);//移除第3个
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            ArrayList al2 = new ArrayList(al.GetRange(1, 3));//新ArrayList只取旧ArrayList一部份
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            Console.WriteLine("遍历方法一:");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            foreach (int i in al)//不要强制转换
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif            ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                Console.WriteLine(i);//遍历方法一
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif            }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            Console.WriteLine("遍历方法二:");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            for (int i = 0; i != al2.Count; i++)//数组是length
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif            ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                int number = (int)al2;//一定要强制转换
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                Console.WriteLine(number);//遍历方法二
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif            }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
页: [1]
查看完整版本: C#的6种常用集合类大比拼【月儿原创】