集合(非泛型,泛型)



ArrayList(非泛型集合,弱类型,较不推荐)

以下是ArrayList示例:


using System;
using System.Collections;//使用ArrayList引用相应空间
namespace ConsoleApp021{
class Program {
//循环枚举输出ArrayList数组元素成员
public static void Log(ArrayList arr) {
string str = "当前数组有(" + arr.Count + ")个元素:";
for (int i = 0; i < arr.Count; i++) {
str += "[" + i + "]:";
str += arr[i] + "; ";
}
Console.WriteLine(str);
}
static void Main(string[] args)
{
//创建对象
ArrayList arr = new ArrayList();
Log(arr);//查看整个数组元素
//添加元素:使用Add方法在队尾添加元素,
arr.Add(15); //下标0
arr.Add(11.11f); //下标1
arr.Add("ArrayList示例");//下标2
arr.Add("老王"); //下标3
//通过元素坐标访问元素
//Console.WriteLine("arr[0] = " + arr[0]);
//Console.WriteLine("arr[1] = " + arr[1]);
//Console.WriteLine("arr[2] = " + arr[2]);
////Console.WriteLine("arr[10] = " + arr[10]);出错!引用不存在元素!
Log(arr);//查看整个数组元素
//Count方法获取元素数量
int count = arr.Count;
//插入元素:使用insert()方法在指定下标处插入元素
//!注意:不可插入比"当前最大下标"大的下标(超出范围)
arr.Insert(1,"老李");
Log(arr);//查看整个数组元素
//删除元素:使用remove方法删除指定元素
arr.Remove("老王");
Log(arr);//查看整个数组元素
//删除指定位置(下标)的元素:使用removeat方法删除指定位置的元素
arr.RemoveAt(0);
Log(arr);//查看整个数组元素
//确认元素是否存在:contains方法确认指定元素是否在数组中存在
bool b = arr.Contains("老李");
if (b) {
Console.WriteLine("数组中存在[老李]");
}
else {
Console.WriteLine("数组中不存在[老李]"); }
//删除所有元素:clear方法
arr.Clear();
Log(arr);//查看整个数组元素
Console.ReadKey();
}
}
}
List(泛型集合,强类型,推荐)



using System;
using System.Collections;//ArrayList需要引用的命名空间
using System.Collections.Generic;//List泛型集合需要引用的命名空间
namespace ConsoleApp022{
class Program {
static void Main(string[] args) {
//声明List对象
List<string> arr = new List<string>();
//注意:ArrayList(弱类型)可以添加各种类型元素,List(强类型)只能添加指定类型元素
//添加元素:使用add()方法
arr.Add("你好");
arr.Add("世界");
arr.Add("哈哈哈");
//arr.Add(250);//错误:List集合arr已被声明为string类型,故不能添加其他类型元素
//插入元素:使用insert()方法在指定位置插入元素
arr.Insert(1, "君子好逑");
//删除元素:使用Remove方法删除一个(已存在)指定元素
arr.Remove("哈哈哈");
//删除元素:使用RemoveAt方法删除指定下标位置的元素
arr.RemoveAt(0);//删除第0个下标的元素
//元素个数:使用Count属性获取List的元素个数.
Console.WriteLine("arr共有" + arr.Count + "个元素.");
//是否包含元素:使用Contains方法确定List中是否包含指定元素
bool b = arr.Contains("世界");//确定arr中是否包含"世界"
arr[0] = "随便测试";//给指定下标元素赋值
string str = arr[1];//获取指定元素值
//清空所有元素:使用Clear方法清空List中所有元素
arr.Clear();
//ArrayList和List的区别
ArrayList a = new ArrayList();
//ArrayList弱类型,对元素类型没有限制;List强类型,元素类型必须相同
a.Add("份文件啊发了");
a.Add(123f);
a.Add(250);
//因为ArrayList对元素类型不限制,系统会把元素当成object类型对象存储
//string s = a[0];//错误的元素取值方法:ArrayList的元素类型是object
string s = (string)a[0];//正确:必须将object类型强制转换为string
Console.Read();
}
}
}
字典(泛型)


using System;
using System.Collections.Generic;//泛型集合命名空间
namespace ConsoleApp023 {
class Program {
static void Main(string[] args) {
//创建字典对象key为string类型,value为int类型
Dictionary<string,int> dic = new Dictionary<string, int>();
//add方法添加元素 - 键值对
dic.Add("老王",53);
dic.Add("小李", 19);
dic.Add("老猪", 300);
int age = dic["老王"];//通过key获取value
int count = dic.Count;//count属性获取元素个数
//ContainsKey方法检查字典是否包含指定key键
bool b = dic.ContainsKey("老猪");//字典中是否包含键为"老猪"的元素
//ContainsValue方法检查字典是否包含指定value值
bool c = dic.ContainsValue(300);//字典中是否有值等于300的元素
//尝试获取指定key对应的value
int s;
bool x = dic.TryGetValue("老王", out s);
//如果字典中有"老王"key键,则获取对应的value值到s, x=true
//如果字典中没有"老王"key键,那么s=null, x=false
//remove方法删除元素 - 键值对
dic.Remove("老王");//删除元素"老王"键值对
//clear方法清空字典-即所有元素
dic.Clear();
}
}
}
栈和队列(泛型)




using System;
using System.Collections.Generic;//引用泛型所需命名空间
using System.Linq;
using System.Text;
namespace ConsoleApp024{
class Program {
static void Main(string[] args) {
//创建栈(类似投井,先进后出-后进先出)
Stack<string> s = new Stack<string>();
//常用的一些方法和属性类似其他集合
int count = s.Count;
s.Clear();
bool b = s.Contains("小王");
//Push:栈中添加元素
//stack独有的:push-入栈(类似跳井)
s.Push("小王");
s.Push("小张");
s.Push("小明");
//stack独有的:pop-(弹)出栈(类似从井里1个个救人)
//获取元素,类似投井后救人,先进后出-后进先出
Console.WriteLine(s.Pop());//小明
Console.WriteLine(s.Pop());//小张
Console.WriteLine(s.Pop());//小王
//创建队列-类似排队,先进先出,后进后出
Queue<string> q = new Queue<string>();
//队列常用方法和属性,类似其他集合
q.Clear();
int c = q.Count();
bool bb = q.Contains("老王");
//queue独有:向队列中插入元素,类似排队
q.Enqueue("老张");
q.Enqueue("老王");
q.Enqueue("老李");
//queue独有:获取元素,类似排队后出队:按顺序先进先出
Console.WriteLine(q.Dequeue());//老张
Console.WriteLine(q.Dequeue());//老王
Console.WriteLine(q.Dequeue());//老李
Console.ReadKey();
}
}
}
本文深入探讨了非泛型和泛型集合的概念,包括ArrayList、List、字典、栈和队列的使用方法及特性。对比了ArrayList与List的类型安全性,演示了如何在C#中操作这些集合。
1641

被折叠的 条评论
为什么被折叠?



