System.Collections.Generic
List list = new List();
ArrayList
一样
list.Capacity
list.Count
list.Add(t)
list.AddRange(list2)
list.Insert(index, t)
list.Remove(t)
list.Remove(index)
list.Clear()
list[index] = t
list[index]
list.Contains(t)
list.IndexOf(t)
list.LastIndexOf(t)
// for 循环
for(int i = 0; i < list.Count; i++){
Console.WriteLine(list[0]);
}
// foreach 循环
foreach(var item in list){
Console.WriteLine(item);
}
HashTable
,也是基于键的哈希值组织起来的 键值对Dictionary dictionary= new Dictionary();
HashTable
一样
dictionary.Count
dictionary.Add(k, v)
dictionary.Remove(k)
dictionary.Clear()
dictionary[k] = v
dictionary[k]
;找不到k直接报错
dictionary.ContainsKey(k)
dictionary.ContainsValue(v)
// 遍历所有 键
foreach(var key in dictionary.Keys)
{
Console.WriteLine(key);
Console.WriteLine(dictionary[key]);
}
// 遍历所有 值
foreach(var value in dictionary.Values)
{
Console.WriteLine(value);
}
// 遍历所有 键值对
foreach(KeyValuePair<K, V> item in dictionary)
{
Console.WriteLine(item.Key);
Console.WriteLine(item.Value);
}
// 迭代器遍历
IDictionaryEnumerator ite = dictionary.GetEnumerator();
while(ite.MoveNext()){
Console.WriteLine(ite.Key);
Console.WriteLine(ite.Value);
}
LinkedList linkedList = new LinkedList();
LinkedListNode
Next
:下一个节点Previous
:上一个节点linkedList.AddLast(t)
linkedFirst.AddRange(t)
linkedList.AddBefore(before, t)
linkedList.AddAfter(after, t)
linkedList.RemoveFirst()
linkedList.RemoveLast()
linkedList.Remove(t)
linkedList.Clear()
节点.Value = t
):linkedList.First.Value = t
linkedList.First
linkedList.Last
linkedList.Find(t)
linkedList.Contains(t)
// foreach 遍历
foreach(var value in linkedList)
{
Console.WriteLine(value);
}
// 通过节点遍历 - 从头到尾
LinkedListNode<T> node = linkedList.First;
do {
Console.WriteLine(node);
node = node.Next;
} whiel(node != null);
// 通过节点遍历 - 从尾到头
LinkedListNode<T> node = linkedList.Last;
do {
Console.WriteLine(node);
node = node.Previous;
} whiel(node != null);
和普通的栈和队列用法完全一致,只是多了泛型而已
T[]
,只是封装了特殊存储规则:先进后出Stack stack = new Stack();
stack.Count
stack.Push(t)
stack.Pop()
stack.Clear()
stack.Peek()
stack.Contains(t)
// foreach 循环遍历(迭代器遍历)
forearch(var item in stack)
{
Console.WriteLine(item);
}
// 转为 object[] 进行 for 遍历
object[] arr = stack.ToArray();
for(int i = 0; i < arr.Length; i++){
Console.WriteLine(arr[i]);
}
// 循环弹栈
while(stack.Count > 0){
object item = stack.Pop();
Console.WriteLine(item);
}
T[]
,只是封装了特殊存储规则:先进先出Queue que= new Queue();
que.Count
que.Enqueue(t)
que.Dequeue()
que.Clear()
que.Peek()
que.Contains(t)
// foreach 循环遍历(迭代器遍历)
forearch(var item in que)
{
Console.WriteLine(item);
}
// 转为 object[] 进行 for 遍历
object[] arr = que.ToArray();
for(int i = 0; i < arr.Length; i++){
Console.WriteLine(arr[i]);
}
// 循环弹栈
while(que.Count > 0){
object item = que.Dequeue();
Console.WriteLine(item);
}