C# 数据结构(DataStructure)
ConcurrentQueue: 线程安全版本的Queue;
ConcurrentStack: 线程安全版本的Stack;
ConcurrentBag: 线程安全的对象集合,一个无序的数据结构集,当不需要考虑顺序时非常有用;
ConcurrentDictionary: 线程安全的Dictionary;
BlockingCollection: 与经典的阻塞队列数据结构类似;
一:数组
Array:在内存上连续分配的,而且元素类型是一样的,可以坐标访问;
读取快但是增删慢,总长度不变。
public void ArrayFun()
{
Console.WriteLine("***************Array******************");
int[] intArray = new int[3];
intArray[0] = 123;
intArray[1] = 456;
intArray[2] = 123;
// 根据索引获取数据
int a = intArray[0];
// 这种长度必须按照制定的长度[3, 4]来
int[,] aa = new int[3, 4] {
{
0, 1, 2, 3} , /* 初始化索引号为 0 的行 */
{
4, 5, 6, 7} , /* 初始化索引号为 1 的行 */
{
8, 9, 10, 11} /* 初始化索引号为 2 的行 */
};
// 这种长度可以随意
int[][] abc = new int[2][];
abc[0] = new int[] {
1, 2 };
abc[1] = new int[] {
3, 4, 5, 6 };
string[] stringArray = new string[] {
"123", "234" };
}
ArrayList:不定长的,连续分配的;
元素没有类型限制,任何元素都是当成object处理,如果是值类型,会有装箱操作;
读取快–增删慢;
/// <summary>
/// ArrayList 不定长的,连续分配的;
/// <para>元素没有类型限制,任何元素都是当成object处理,如果是值类型,会有装箱操作</para>
/// <para>读取快--增删慢</para>
/// </summary>
public void ArrayListFun()
{
Console.WriteLine("***************ArrayList******************");
ArrayList arrayList = new ArrayList();
arrayList.Add("aaa");
arrayList.Add("Is");
arrayList.Add(32);//add增加长度
// 只能add
//arrayList[4] = 26;
//删除数据
//arrayList.RemoveAt(4);
var value = arrayList[2];
arrayList.RemoveAt(0);
arrayList.Remove("aaa");
}
List:也是Array,内存上都是连续摆放;
不定长;泛型,保证类型安全,避免装箱拆箱;
读取快–增删慢;
/// <summary>
/// List:也是Array,内存上都是连续摆放;不定长;泛型,保证类型安全,避免装箱拆箱
/// <para>读取快--增删慢</para>
/// </summary>
public void ListFun()
{
Console.WriteLine("***************List<T>******************");
List<int> intList = new List<int>() {
1, 2, 3, 4 };
intList.Add(123);
intList.Add(123);
//intList.Add("123");
//intList[0] = 123;
List<string> stringList = new List<string