赞
踩
看了c#提供的数据结构的源码后,也清晰了各个数据结构的优缺点,也是面试或工作都必须要掌握的东西,希望我的总结能帮到你们。
//初始化
int[] intArray = new int[5];
//赋值
intArray[0] = 1;
intArray[1] = 1;
intArray[2] = 1;
intArray[3] = 1;
intArray[4] = 1;
//会报错超出索引,且Array不会自动扩容
intArray[5] = 1;
//会报错,存放的类型也必须一致
intArray[6] = "文字";
//初始化化
ArrayList arrayList = new ArrayList(0); //不指定大小的初始化,会构造一个空的容器
ArrayList arrayList2 = new ArrayList(1); //指定大小的初始化
ArrayList arrayList3 = new ArrayList(arrayList2); //传入一个继承了ICollection 接口的容器,会将该容器的元素全部复制
//自动扩容:将当前容器乘于二
arrayList2.Add(1); //1
arrayList2.Add(1); //2
arrayList2.Add(1); //4
arrayList2.Add(1); //4
arrayList2.Add(1); //8
Console.WriteLine("扩容后的大小:" + arrayList2.Capacity);
//List是ArrayList的泛型类,声明时需要指定类型,没有拆箱装箱,具有安全类型
List<int> a = new List<int>();
//使用IReadOnlyCollection来实例化List那么只能通过foreach来遍历 因为他继承实现了IEnumerable
IReadOnlyCollection<int> collectionList = new List<int>() { 1, 2, 3 };
IEnumerator<int> ienum = collectionList.GetEnumerator();
foreach (int item in collectionList)
{
Console.WriteLine(item);
}
//IReadOnlyList 可以通过索引来获取对应元素
IReadOnlyList<int> readList = new List<int>() { 1,2,3};
int readEle = readList[1];
Console.WriteLine(readEle);
特点
总结
//双重散列法的函数
private uint InitHash(Object key, int hashsize, out uint seed, out uint incr) {
//获取code
uint hashcode = (uint) GetHash(key) & 0x7FFFFFFF;
seed = (uint) hashcode;
//当hash冲突时会使用incr来重新计算插入的位置
incr = (uint)(1 + ((seed * HashPrime) % ((uint)hashsize - 1)));
return hashcode;
}
//存入的key value 会多出一个hashcode 以及 next指针
//实际存储在字典中的元素
private struct Entry {
public int hashCode; // Lower 31 bits of hash code, -1 if unused
public int next; // Index of next entry, -1 if last
public TKey key; // Key of entry
public TValue value; // Value of entry
}
//将队列中的元素复制到list
Queue<int> intQue = new Queue<int>();
intQue.Enqueue(1);
intQue.Enqueue(2);
intQue.Enqueue(3);
int[] intsList = new int[3];
intQue.CopyTo(intsList, 0);
foreach (var item in intsList)
{
Console.WriteLine("从队列中Copy来的元素:" + item);
}
//1.使用BCL中的Stack<T>
Stack<char> stack = new Stack<char>();
stack.Push('a');//添加数据//先添加的为栈底
stack.Push('b');//添加数据
stack.Push('c');//添加数据//后添加的为栈顶
Console.WriteLine("Count获取stack中的数据个数:"+stack.Count);
char pop = stack.Pop();//取得栈顶的数据并返回
Console.WriteLine("Pop删除并取得栈顶的数据"+pop);
char peek = stack.Peek();
Console.WriteLine("Peek取得栈顶数据"+peek);
LinkedList<int> a = new LinkedList<int>();
//在链表的最后一个新增一个节点
a.AddLast(1);
//在链表的最前方新增一个节点
a.AddFirst(2);
//在指定的节点的前面新增一个节点
a.AddAfter(a.Find(1), 3);
//在指定的节点的后面新增一个节点
a.AddBefore(a.Find(2), 4);
//字节的存储范围为0-255
byte[] bt = { 60 } ;
BitArray bitArray;
bitArray = new BitArray(bt);
foreach (var item in bitArray)
{
Console.WriteLine(item);
//输出为 False False True True True True False False 00111100
}
--代码没什么好看的,敷衍一下~ HashSet<int> hsInt = new HashSet<int>(); hsInt.Add(0); hsInt.Add(3); hsInt.Add(4); hsInt.Remove(0); hsInt.Append(1); hsInt.Append(4); hsInt.Append(7); hsInt.Contains(1); hsInt.RemoveWhere(i => i>=5); foreach (int i in hsInt) { Console.WriteLine(i); }
class Enginner { public String Name { get; set; } public int Age { get; set; } public Enginner(string name, int age) { this.Name = name; this.Age = age; } } class DepartMent :IEnumerable { Enginner[] enginners; public DepartMent() { enginners = new Enginner[3]; enginners[0] = new Enginner("Alan", 23); enginners[1] = new Enginner("Kino", 24); enginners[2] = new Enginner("Bruce", 28); } public IEnumerator GetEnumerator() { return enginners.GetEnumerator(); } } //----自定义一个可遍历的集合类---- Console.WriteLine("----自定义一个可遍历的集合类----"); DepartMent departMent = new DepartMent(); foreach (Enginner item in departMent) { Console.WriteLine(item.Age + " " + item.Name); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。