总目录
C# 语法总目录
集合二 List/Queue
- [1. List](#1. List)
- [2. Queue](#2. Queue)
1. List
List有ArrayList和LinkedList
- ArrayList 类似数组,查找快,插入删除慢(相对)
- LinkedList 类似双向链表,查找慢(相对),插入删除快
//ArrayList
csharp
//ArrayList
ArrayList arrList = new ArrayList();
arrList.Add("andy");
arrList.Add("sandy");
arrList.Add("sunny");
arrList.RemoveAt(1);
arrList.Reverse();
arrList.Insert(1, "tom");
arrList.InsertRange(0, new[] {"times","jerry"});
foreach (var item in arrList)
{
Console.WriteLine(item);
}
/*
times
jerry
sunny
tom
andy
*/
//LinkList
csharp
//LinkList
LinkedList<string> linkArr = new LinkedList<string>();
//从头部加入
linkArr.AddFirst("andy");
linkArr.AddFirst("sandy");
linkArr.AddFirst("sunny");
//sunny
//sandy
//andy
//从尾部加入
linkArr.AddLast("tom");
linkArr.AddLast("jerry");
/*
sunny
sandy
andy
tom
jerry
*/
//在顺数第三个位置添加元素
linkArr.AddAfter(linkArr.First.Next, "get");
//在倒数第三个位置添加元素
linkArr.AddBefore(linkArr.Last.Previous, "give");
foreach (var item in linkArr)
{
Console.WriteLine(item);
}
/*
sunny
sandy
get
andy
give
tom
jerry
*/
2. Queue
队列,先进先出
csharp
Queue<string> queue = new Queue<string>();
queue.Enqueue("tom");
queue.Enqueue("jerry");
queue.Enqueue("times");
string deTemp = queue.Dequeue();
Console.WriteLine("dequeue ele is "+deTemp);
Console.WriteLine(queue.Count);
Console.WriteLine(queue.Contains("tom"));
//给容器瘦身
queue.TrimExcess();
string[] strArr = queue.ToArray();
foreach (var item in strArr)
{
Console.WriteLine(item);
}
/*
dequeue ele is tom
2
False
jerry
times
*/
总目录
C# 语法总目录