C#自学笔记:常用集合类

进阶

ArrayList

本质

ArrayList是一个c#为我们封装好的类,本质上是一个objec类型的数组,ArrayList类帮助我们实现了很多方法,比如数组的增删改查。

使用

using System.Collections;

ArrayList array = new ArrayList();

//增
array.Add(1);
array.Add("123");
array.Add(true);
array.Add(new object());
array.Add(new Test());
ArrayList array2 = new ArrayList();
array2.Add(123);
//范围增加(批量增加,把另一个list容器里面的内容加到后面)
array.AddRange(array2);
//从中间插入,第一个参数是下标位置,第二个参数是插入的数据
array.Insert(1, "123456");

//删
//移除指定元素,从头遍历,找到删除
array.Remove(1);
//移除指定位置的元素
array.RemoveAt(2);
array.Clear();

//查
Console.WriteLine(array[0]);
if (array.Contains("123")
{
    return true;
}
//正向查找,找到第一个
int index = array.IndexOf(true);	//此处找到的是数组里的true
Console.WriteLine(index);			//2
//反向查找
index = array.LastIndexOf(true);

//改
array[0] = "999";

遍历

//长度
Console.WriteLine(array.Count);
//容量
Console.WriteLine(array.Capacity);
for (int i = 0; i < array.Count; i++)
{
    Console.WriteLine(array[i]);
}

//迭代器遍历
foreach (object item in array)
{
    Console.WriteLine(item);
}

装箱拆箱

ArrayList本质上是一个可以自动扩容的object数组。

由于用万物之父来存储数据,自然存在装箱拆箱,当往其中进行值类型存储时就是在装箱,当值类型对象取出来转换使用时,就存在拆箱。

所以ArrayList尽量少用,之后我们会学习更好的数据容器。

Stack

本质

Stack是一个c#为我们封装好的类,本质也是object[]数组,只是封装了特殊的存储规则

使用

using System.Collections

Stack stack = new Stack();

//增
//压栈
stack.Push(1);
stack.Push("123");
stack.Push(true);
stack.Push(1.2f);
stack.Push(new Test());

//取
//弹栈
object v = stack.Pop();
Console.WriteLine(v);

//查
//栈无法查看指定位置的元素,只能查看栈顶的内容
v = stack.Peek();
Console.WriteLine(v);//不会弹出,只会查看内容
if (stack.Contains(1.2f)
{
    return true;
}
    
//改
//栈无法改变其中的元素,只能压栈和弹栈
//实在要改,只能清空
stack.Clear();

遍历

//长度
Console.WriteLine(stack.Count);
//由于无法通过下标获取元素,所以无法使用for循环进行遍历

//迭代器遍历
//顺序是从栈顶到栈底
foreach (object item in stack)
{
    Console.WriteLine(item);
}

//还有一种遍历方式
//将栈转换为object数组
object[] array = stack.ToArray();
for (int i = 0; i < array.Length; i++)
{
    Console.WriteLine(array[i]);
}

//循环弹栈
while(stack.Count > 0)
{
    object o = stack.Pop();
    Console.WriteLine(o);
}

装箱拆箱

由于用万物之父来存储数据,自然存在装箱拆箱,当往其中进行值类型存储时就是在装箱,当值类型对象取出来转换使用时,就存在拆箱。

Queue

本质

Queue是一个c#为我们封装好的类

它的本质也是object[]数组,只是封装了特殊的存储规则

使用

using System.Collections

Queue queue = new Queue();

//增
queue.Enqueue(1);
queue.Enqueue("123");
queue.Enqueue(1.4f);
queue.Enqueue(new Test());

//取
//出队
//队列中不存在删除的概念,只有取的概念,取出先加入的对象
object v = queue.Dequeue();
Console.WriteLine(v);

//查
//查看队列头部元素但不会移除
v = queue.Peel();
Console.WriteLine(v);
if (queue.Contains(1.4f))
{
    return true;
}

//改
//队列无法改变其中的元素,只能进出队列
//实在要改,只有清空
queue.Clear();

遍历

//长度
Console.WriteLine(queue.Count);
foreach(object item in queue)
{
    Console.WriteLine(item);
}

//还有一种遍历方式
//将栈转换为object数组
object[] array = queue.ToArray();
for (int i = 0; i < array.Length; i++)
{
    Console.WriteLine(array[i]);
}

//循环出列
while(queue.Count > 0)
{
    object o = queue.Dequeue();
    Console.WriteLine(o);
}

装箱拆箱

由于用万物之父来存储数据,自然存在装箱拆箱,当往其中进行值类型存储时就是在装箱,当值类型对象取出来转换使用时,就存在拆箱。

Hashtable

本质

Hashtable(又称散列表)是基于键的哈希代码组织起来的键/值对

它的主要作用是提高数据查询的效率

使用键来访问集合中的元素

使用

using System.Collections;

Hashtable hashtable = new Hashtable();

//增
//不允许出现相同的键
hashtable.Add(1, "123");
hashtable.Add("123", 1);
hashtable.Add(true, false);
hashtable.Add(false, false);

//删
//只能通过键去删除
//删除不存在的键时没反应
hashtable.Remove(1);
hashtable.Remove(2);
//或者直接清空
hashtable.Clear();

//查
//找不到会返回空
Console.WriteLine(hashtable[1]);
Console.WriteLine(hashtable[4]);//null
//根据键检测
if (hashtable.Contains(2)
{
    Console.WriteLine("存在键为2的键值对");
}
if (hashtable.ContainsKey(2))
{
    Console.WriteLine("存在键为2的键值对");
}
//根据值检测
//如何找到了任何一个匹配的值,则返回true
if (hashtable.ContainsValue(12))
{
    Console.WriteLine("存在值为12的键值对");
}

//改
//只能改键对应的值内容,无法修改键
Console.WriteLine(hashtable[1]);
hashtable[1] = 100.5f;
Console.WriteLine(hashtable[1]);

遍历

//得到键值对的对数
Console.WriteLine(hashtable.Count);

//1. 遍历所有的键
foreach(object item in hashtable.Keys)
{
    Console.WriteLine("键:" + item);
    Console.WriteLine("值:" + hashtable[item]);
}
//2. 遍历所有值
foreach(object item in hashtable.Values)
{
    Console.WriteLine("值:" + item);
}
//3. 键值对一起遍历
foreach(DictionaryEntry item in hashtable)
{
    Console.WriteLine("键:" + item.Key + "值:" + item.Value);
}
//4. 迭代器遍历法
IDictionaryEnumerator my Enumerator = hashtable.GetEnumerator();
bool flag = myEnumerator.MoveNext();
while(flag)
{
    Console.WriteLine("键:" + myEnumerator.Key + "值:" + myEnumerator.Value);
    flag = myEnumerator.MoveNext();
}

装箱拆箱

由于用万物之父来存储数据,自然存在装箱拆箱,当往其中进行值类型存储时就是在装箱,当值类型对象取出来转换使用时,就存在拆箱。
posted @ 2025-07-14 18:42  柠凉w  阅读(13)  评论(0)    收藏  举报