一、C#简单数据结构类
1.ArrayList(数组表)
知识点一 ArrayList的本质
ArrayList是一个C#为我们封装好的类
它的本质是一个object类型的数组(可以存储任何对象)
ArreyList帮我们封装好了数组的增删查改
知识点二 申明
1 2
| ArrayList array = new ArrayList();
|
知识点三 增删查改
增
1 2 3 4 5 6 7 8 9 10 11 12 13
| array.Add(1); array.Add("123"); array.Add(true); array.Add(new object()); array.Add(new Test()); array.Add(1);
ArrayList array2 = new ArreyList(); array.AddRange(array2);
array.Insert(int index,object? value ); array.Insert(1,"999");
|
删
1 2 3 4 5 6
| array.Remove(1);
array.RemoveAt(0);
array.clear();
|
查
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| Console.WriteLine(array[0]);
if(array.Contains("123")) { Console.WriteLine("123 exist!"); }
int index = array.IndexOf(ture); Console.WriteLine(index); Console.WriteLine(array.IndexOf(false));
index = array.LastIndexOf(true); Console.WriteLine(index);
|
改
遍历
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| Console.WriteLine(array.Count);
Console.WriteLine(array.Capacity);
for(int i;i < array.Count;i++) { Console.WriteLine(array[i]); }
foreach(object item in array) {
}
|
知识点四 装箱拆箱
ArrayList本质上是一个可以自动扩容的object数组。
由于用万物之父来存储数据,自然就存在装箱拆箱。
当往其中进行值类型存储时就是在装箱,当将值类型对象取出来使用时,就存在拆箱。
所以ArreyList尽量少用,尽量用更好的数据容器(stack和heap内存相互转换)
1 2 3
| int num = 1; array[0] = num; num = (int)array[0];
|
2.Stack(栈)
知识点一 Stack的本质
Stack(栈)同样是C#为我们封装好的一个类,同样是一个有object类组成的数组,但是封装了独特的进出规则,具体情况可以参考手枪的单排弹匣
知识点二 Stack的声明
1 2
| Stack stack = new Stack();
|
知识点三
知识点四
知识点五
3.Queue(队列)
4.Hashtable(哈希表)