
已解决问题
谷歌helene1230用户在2015.10.03提交了关于“十二生肖c#中Dictionary,ArrayList,h**ashtable和数组 Array 的区别”的提问,欢迎大家涌跃发表自己的观点。目前共有1个回答,最后更新于2024-10-24T09:50:23。希望大家能够帮助她。详细问题描述及疑问:期待您的答案,你是我的宝贝,你是我的花,谢谢你对我的帮助!
详细问题描述及疑问:期待您的答案,你是我的宝贝,你是我的花,谢谢你对我的帮助!
C#集合类A
1.数组侍定大小的,不能伸缩。虽然System.Array.Res
但是该方法是重新创建新设置大小的数组,用的是旧数组的元素初始化。随后以前的数组就废弃!而集合却是可变长的
2.数组要声明元素的类
3.数组可读可写不能声明只读数组。集合类可以
4.数组要
很多时候集合有定制的下标类型,对于队列和栈根本就不支持下标访问!
//
int[]intArra
//初始化已声明的一维数组
int
intArray1=newint[3]{1,2,3};
intAr
//Arr
1:Add()向数组中添加一个元素,
2:Remove()删
3:RemoveAt(inti)删除数组中
4:Reverse()
5:Sort()以从小到
6:Clone()复制一个数
//List
可通过索引访问的对象的强类型列表。提供用于对列表进行搜索、排序和操作的方法
在决定使用List
两个类的
如果对类型T使用
元素之后,不对列表元素装箱所节省的内存将大于生成该类实现所使用
//Dictionary
表
//SortedL
与哈希表类似,区别在于SortedList中的Key数组排好序的
//h**ashtable
哈希表,名-衷。类似于字典(
Geth**ashCode()方法返回一
//Stack类
栈,后进先出。push方法入栈,pop方法出栈。
Queue类
队列,先进先出。enqueue方法入队列,dequeue方法出队列。
-------------------------------------------------------------
//Dictionary
System.Collections.DictionaryEntrydic=newSystem.Collections.DictionaryEntry("key1","value1");
Dictionary<int,string>fruit=newDictionary<int,string>();
//加入重复键会引发异常
fruit.Add(1,"苹果");
fruit.Add(2,"桔子");
fruit.Add(3,"香蕉");
fruit.Add(4,"菠萝");
//因为引入了泛型,所以键取出后不需要进行Object到int的转换,值的集合也一样
foreach(intiinfruit.Keys)
{
Console.WriteLine("键是:{0}值是:{1}",i,fruit);
}
//删除指定键,值
fruit.Remove(1);
//判断是否包含指定键
if(fruit.ContainsKey(1))
{
Console.WriteLine("包含此键");
}
//**集合中所有对象
fruit.Clear();
}
//ArrayList
System.Collections.ArrayListlist=newSystem.Collections.ArrayList();
list.Add(1);
list.Add(2);
for(inti=0;i<list.Count;i++)
{
System.Console.WriteLine(list[i]);
}
//List
//声明一个List对象,只加入string参数
List<string>names=newList<string>();
names.Add("乔峰");
names.Add("欧阳峰");
names.Add("马蜂");
//遍历List
foreach(stringnameinnames)
{
Console.WriteLine(name);
}
//向List中插入元素
names.Insert(2,"张三峰");
//移除指定元素
names.Remove("马蜂");
//h**ashTable
System.Collections.h**ashtabletable=newSystem.Collections.h**ashtable();
table.Add("table1",1);
table.Add("table2",2);
System.Collections.IDictionaryE**meratord=table.GetE**merator();
while(d.MoveNext())
{
System.Console.WriteLine(d.Entry.Key);
}
//Queue
System.Collections.Queuequeue=newSystem.Collections.Queue();
queue.Enqueue(1);
queue.Enqueue(2);
System.Console.WriteLine(queue.Peek());
while(queue.Count>0)
{
System.Console.WriteLine(queue.Dequeue());
}
//SortedList
System.Collections.SortedListlist=newSystem.Collections.SortedList();
list.Add("key2",2);
list.Add("key1",1);
for(inti=0;i<list.Count;i++)
{
System.Console.WriteLine(list.GetKey(i));
}
//Stack
System.Collections.Stackstack=newSystem.Collections.Stack();
stack.Push(1);
stack.Push(2);
System.Console.WriteLine(stack.Peek());
while(stack.Count>0)
{
System.Console.WriteLine(stack.Pop());
}