.net 集合类初步认识
先来说说数组的不足(也可以说集合与数组的区别):(这是我看得别人的这段)
1.数组是固定大小的,不能伸缩。虽然System.Array.Resize这个泛型方法可以重置数组大小,但是该方法是重新创建新设置大小的数组,用的是旧数组的元素初始化。随后以前的数组就废弃!而集合却是可变长的.
2.数组要声明元素的类型,集合类的元素类型却是object.
3.数组可读可写不能声明只读数组。集合类可以提供ReadOnly方法以只读方式使用集合。
4.数组要有整数下标才能访问特定的元素,然而很多时候这样的下标并不是很有用。集合也是数据列表却不使用下标访问。很多时候集合有定制的下标类型,对于队列和栈根本就不支持下标访问.
此次试验总结:
1.以键值对形式存在的:
HashTable,key必须唯一,不唯一异常
NameValueCollection,key可以不唯一,则通过key得到和这个key关联的所有value
SortedList,key唯一,但是重复插入相同key,old value被new value覆盖,也就是降频的key唯一
以单object形式存在的:Arraylist,Queue,Stack
2.支持下标访问的:
HashTable[key],得到key对应的value
NameValueCollection[ksy],得到key对应的values们
SortedList[ksy],得到key对应的value
支持索引下标访问的:
Arraylist[i],得到i位置的object ,下标越界出异常
不支持下标访问的:
Queue,Stack
测试代码:
1
/// <summary>
2
/// 哈希表,名-值对。类似于字典(比数组更强大)。
3
/// 哈希表是经过优化的,访问下标的对象先散列过。
4
/// 如果以任意类型键值访问其中元素会快于其他集合。
5
/// GetHashCode()方法返回一个int型数据,使用这个键的值生成该int型数据。
6
/// 哈希表获取这个值最后返回一个索引,表示带有给定散列的数据项在字典中存储的位置。
7
/// </summary>
8
class Program
9
{
10
static void Main(string[] args)
11
{
12
Hashtable ht = new Hashtable();//15重载
13
ht.Add("one",1);//add 方法添加元素 必须符合 key:value 的形式 kay唯一,不唯一异常
14
ht.Add("four", new int[] {1,2,3});
15
ht.Add("two", "2");
16
ht.Add("three", "three");
17
PrintKeyAndValues(ht);
18
Console.WriteLine("hashtable is count {0}",ht.Count);
19
//ht.Remove("two");//通过key移除一个元素
20
//Console.WriteLine("ht.Remove('two') hashtable is count {0},now key_two value is {1}", ht.Count,ht["two"]);//下标访问方法,参数key得到value 或者设置value
21
22
///hashtable排序
23
//ArrayList list = new ArrayList(ht.Keys);
24
//list.Sort();
25
PrintKeyAndValues(ht);
26
Console.ReadLine();
27
}
28
public static void PrintKeyAndValues(Hashtable ht)
29
{
30
foreach (DictionaryEntry dictory in ht)//dictionaryentry 基于key value的基元
31
{
32
Console.WriteLine("key:{0},value:{1}",dictory.Key,dictory.Value);
33
}
34
foreach (string key in ht.Keys)//遍历
35
{
36
Console.WriteLine(key);
37
}
38
}
39
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
1
/// <summary>
2
/// HashTable 和 NameValueCollection很类似但是他们还是有区别的,
3
/// HashTable 的KEY是唯一性,而NameValueCollection则不唯一!
4
/// </summary>
5
class Program
6
{
7
static void Main(string[] args)
8
{
9
//Hashtable 中的key唯一,重复会出异常
10
System.Collections.Hashtable ht = new System.Collections.Hashtable();
11
ht.Add("DdpMDisplaySeq".Trim(), "Display Sequence".Trim());
12
ht.Add("DdpMNameChi".Trim(), "Name (Chinese)".Trim());
13
ht.Add("DdpMNameEng".Trim(), "Name (English)".Trim());
14
ht.Add("Comment".Trim(), "Comment".Trim());
15
ht.Add("DdpMMarketCode".Trim(), "Market Code".Trim());
16
foreach (object key in ht.Keys)
17
{
18
Console.WriteLine("{0}/{1} {2},{3}", key, ht[key], key.GetHashCode(), ht[key].GetHashCode());
19
}
20
Console.WriteLine(" ");
21![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
NameValueCollection myCol = new NameValueCollection();
23
myCol.Add("DdpMDisplaySeq".Trim(), "Display Sequence".Trim());
24
myCol.Add("DdpMNameChi".Trim(), "Name (Chinese)".Trim());
25
myCol.Add("DdpMNameChi".Trim(), "Name (English)".Trim());
26
myCol.Add("Comment".Trim(), "Comment".Trim());
27
myCol.Add("DdpMMarketCode".Trim(), "Market Code".Trim());
28
foreach (string key in myCol.Keys)//如果一个key有多个value,那么myCol[key]会得到所有的value
29
{
30
Console.WriteLine("{0}/{1} {2},{3}", key, myCol[key], key.GetHashCode(), myCol[key].GetHashCode());
31
}
32
myCol.Set("hello","world");//set 和 add 效果相同
33
//myCol.Remove(key); 移除
34
//myCol.Get() 2重载 参数 index 参数keyname
35
Console.ReadLine();
36
}
37
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
1
/// <summary>
2
/// queue 队列用例 队列,先进先出。enqueue方法入队列,dequeue方法出队列。
3
/// </summary>
4
class Program
5
{
6
static void Main(string[] args)
7
{
8
Queue qu1 = new Queue();
9
Queue qu2 = new Queue(6);
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
foreach (int i in new int[5]{1,2,3,4,5})
12
{
13
qu1.Enqueue(i);//enqueue 方法添加元素
14
qu2.Enqueue(i);
15
}
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
foreach (object var in qu1)//遍历 不提供下标引用
18
{
19
Console.Write(var);
20
}
21
Console.WriteLine();
22
object o1 = qu1.Dequeue();//弹出队列开始处的对象,永久弹出,删除了
23
Console.WriteLine("qu1 contains o1 {0}",qu1.Contains(o1));
24
Console.WriteLine();
25
object o2 = qu2.Peek();//弹出队列开始处的对象
26
Console.WriteLine("qu2 contains o1 {0}",qu2.Contains(o2));
27![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
Console.WriteLine("qu2 count is {0}",qu2.Count);
29
qu2.Enqueue("A");
30
qu2.Enqueue("B");//添加元素,count超过初始值 +1
31
Console.WriteLine("qu2 count is {0}", qu2.Count);
32
Console.ReadLine();
33
}
34
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
1
/// <summary>
2
/// 哈希表类似,区别在于SortedList中的Key数组排好序的
3
/// </summary>
4
class Program
5
{
6
static void Main(string[] args)
7
{
8
SortedList list = new SortedList(4);//6重载实现
9
list.Add("one",1);//添加元素的两种方法
10
list["zhang"] = 2;
11
list["three"]=3;
12
list["four"] = "this";
13
list["four"] = "this1";//重复key this1 覆盖掉this 不出异常
14
Console.WriteLine("list is capcity {0}", list.Capacity);//容量 默认16
15
Console.WriteLine("list is count {0}", list.Count);//实际内容大小
16
17
list.Add("five",1);
18
Console.WriteLine("add one element");
19
Console.WriteLine("list is capcity {0}", list.Capacity);
20
Console.WriteLine("list is count {0}", list.Count);
21
22
foreach (DictionaryEntry dictionary in list)//遍历
23
{
24
Console.WriteLine("key is {0},value is {1}",dictionary.Key,dictionary.Value);
25
}
26![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
Console.WriteLine("包含key one 吗?{0}",list.ContainsKey("one"));
28
Console.WriteLine("包含key zz 吗?{0}", list.ContainsKey("zzz"));
29
Console.WriteLine("包含value 1 吗?{0}", list.ContainsValue(1));
30
Console.WriteLine("包含value 5 吗?{0}", list.ContainsValue(5));
31![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
Console.ReadLine();
33
}
34
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
1
/// <summary>
2
/// stack 栈,后进先出。push方法入栈,pop方法出栈。content is object
3
/// </summary>
4
class Program
5
{
6
static void Main(string[] args)
7
{
8
Stack sk1 = new Stack();
9
Stack sk2 = new Stack(6);//设置初始大小
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
foreach (int i in new int[4] { 1, 2, 3, 4 })
12
{
13
sk1.Push(i);//push 方法添加一个元素
14
sk2.Push(i);
15
}
16
//Console.WriteLine(sk1[i]);栈不提供下标访问元素的功能
17
foreach (object var in sk1)//遍历栈
18
{
19
Console.WriteLine(var);
20
}
21
22
object o1=sk1.Pop();//最顶层的对象被弹出,彻底删除
23
Console.WriteLine("Pop is {0}",o1);
24
Console.WriteLine("01 还在吗?{0}",sk1.Contains(o1));//判断是否存在一个object,可以给一个确定的值 eg:1
25
26
object o2=sk2.Peek();//弹出最顶层对象,但是删除
27
Console.WriteLine("Peek is {0}", o2);
28
Console.WriteLine("02 还在吗?{0}",sk2.Contains(o2));
29
30
Console.WriteLine("sk2 count now is {0}", sk2.Count);
31
sk2.Push("a");
32
sk2.Push("b");
33
sk2.Push("c");//测试初始栈大小被改变,增加1不会翻倍,栈没有容量只有count
34
Console.WriteLine("sk2 count now is {0}",sk2.Count);
35
36
while (sk2.Count != 0)//清空栈的方法
37
{
38
sk2.Pop();
39
}
40
Console.WriteLine("sk2 count now is {0}", sk2.Count);
41
Console.ReadLine();
42
}
43
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
1
/// <summary>
2
/// arraylist demo
3
/// </summary>
4
class Program
5
{
6
static void Main(string[] args)
7
{
8
///arraylist集合内容都是object 可以添加任何内容
9
ArrayList list = new ArrayList();//new ArrayList(10)初始化大小,默认16,容量满后,容量是当前容量的两倍
10
list.Add("A");//单个添加元素
11
foreach (int num in new int[] { 1, 2, 3, 4, 5 })
12
{
13
list.Add(num);
14
}
15
string[] arrStr = new string[3];
16
arrStr[0] = "b";
17
arrStr[1] = "c";
18
arrStr[2] = "d";
19
list.AddRange(arrStr);//添加一组元素
20
foreach (object obj in list)
21
{
22
Console.Write(obj.ToString());
23
}
24
list.Remove("A");//移除一个特定元素
25
list.RemoveAt(1);//移除特定位置的元素
26
list.RemoveRange(3,2);//移除一批元素,开始位置,移除个数
27
Console.WriteLine();
28
foreach (object obj in list)//遍历方法一
29
{
30
Console.Write(obj.ToString());
31
}
32
Console.WriteLine();
33
for (int i = 0; i < list.Count; i++)//遍历方法二
34
{
35
Console.Write(list[i]);//也可以使用下标,很像数组阿
36
}
37
Console.WriteLine();
38
ArrayList al2 = new ArrayList(list.GetRange(1, 3));//创建arrlist的另一种方法,初始化一个组给他
39
for (int i = 0; i < al2.Count; i++)
40
{
41
Console.Write(al2[i]);
42
}
43
Console.WriteLine("list 可包含多少元素{0}",list.Capacity);//list容量
44
Console.WriteLine("list 实际包含多少元素{0}", list.Count);//list实际包含元素个数
45
Console.ReadLine();
46
}
47
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
另外对于hashtable认识还不够,hashtable为什么好?objet.GetHasCode()怎么生成的阿?谁给讲讲。