实例介绍
【实例截图】
【核心代码】
using System;
using System.Collections;//引入非泛型集合命名空间(在.NET1.1版本使用)
using System.Collections.Generic;//(.NET2.0以后出现泛型,建议集合都是用泛型集合)
using System.Linq;
using System.Text;
namespace Demo
{
class Program
{
#region 示例1:ArrayList的使用
static void Main(string[] args)
{
//实例化集合对象
ArrayList list = new ArrayList();
//添加对象元素
Student objStu1 = new Student() { Age = 20, StuId = 1001, StuName = "小张" };
Student objStu2 = new Student() { Age = 22, StuId = 1003, StuName = "小李" };
Student objStu3 = new Student() { Age = 22, StuId = 1002, StuName = "小王" };
list.Add(objStu1);
list.Add(objStu2);
list.Add(objStu3);
//遍历元素
for (int i = 0; i < list.Count; i )
{
Student obj = (Student)list[i];
Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", obj.StuName, obj.Age, obj.StuId);
}
//删除元素
list.RemoveAt(1);
Console.WriteLine("删除后元素总数:{0}", list.Count);
Console.ReadLine();
}
#endregion
#region 演示:使用索引查找元素的方法的局限性
//static void Main(string[] args)
//{
// //实例化集合对象
// ArrayList list = new ArrayList();
// //添加对象元素
// Student objStu1 = new Student() { Age = 20, StuId = 1001, StuName = "小张" };
// Student objStu2 = new Student() { Age = 22, StuId = 1003, StuName = "小李" };
// Student objStu3 = new Student() { Age = 22, StuId = 1002, StuName = "小王" };
// list.Add(objStu1);
// list.Add(objStu2);
// list.Add(objStu3);
// Student obj = (Student)list[1];
// Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", obj.StuName, obj.Age, obj.StuId);
// //删除元素
// list.RemoveAt(0);
// obj = (Student)list[0];
// Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", obj.StuName, obj.Age, obj.StuId);
// Console.ReadLine();
//}
#endregion
#region 示例2:使用关键字查找元素的Hashtable集合
//static void Main(string[] args)
//{
// //实例化集合对象
// Hashtable stus = new Hashtable();
// //添加对象元素
// Student objStu1 = new Student() { Age = 20, StuId = 1001, StuName = "小张" };
// Student objStu2 = new Student() { Age = 22, StuId = 1003, StuName = "小李" };
// Student objStu3 = new Student() { Age = 22, StuId = 1002, StuName = "小王" };
// stus.Add("小张",objStu1);
// stus.Add("小李", objStu2);
// stus.Add("小王", objStu3);
// Student obj = (Student)stus["小李"];//通过关键字查找对象
// Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", obj.StuName, obj.Age, obj.StuId);
// //删除元素
// stus.Remove("小张");
// obj = (Student)stus["小李"];//查找元素的方式不变
// Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", obj.StuName, obj.Age, obj.StuId);
// Console.ReadLine();
//}
#endregion
#region 示例3:Hashtable元素的遍历
//static void Main(string[] args)
//{
// //实例化集合对象
// Hashtable stus = new Hashtable();
// //添加对象元素
// Student objStu1 = new Student() { Age = 20, StuId = 1001, StuName = "小张" };
// Student objStu2 = new Student() { Age = 22, StuId = 1003, StuName = "小李" };
// Student objStu3 = new Student() { Age = 22, StuId = 1002, StuName = "小王" };
// stus.Add("小张", objStu1);
// stus.Add("小李", objStu2);
// stus.Add("小王", objStu3);
// //遍历key
// foreach (string item in stus.Keys)
// {
// Console.WriteLine(item);
// }
// //遍历values
// foreach (object item in stus.Values)
// {
// Student obj = (Student)item;
// Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", obj.StuName, obj.Age, obj.StuId);
// }
// Console.ReadLine();
//}
#endregion
#region 示例4:ArrayList数据类型安全问题
//static void Main(string[] args)
//{
// //实例化集合对象
// ArrayList list = new ArrayList();
// //添加对象元素
// Student objStu1 = new Student() { Age = 20, StuId = 1001, StuName = "小张" };
// Student objStu2 = new Student() { Age = 22, StuId = 1003, StuName = "小李" };
// Student objStu3 = new Student() { Age = 22, StuId = 1002, StuName = "小王" };
// list.Add(objStu1);
// list.Add(objStu2);
// list.Add(objStu3);
// //添加教师对象
// list.Add(new Teacher() { Name = "王老师", Age = 28 });
// //遍历元素
// for (int i = 0; i < list.Count; i )
// {
// Student obj = (Student)list[i];
// Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", obj.StuName, obj.Age, obj.StuId);
// }
// Console.ReadLine();
//}
#endregion
#region 示例5:ArrayList数据类型安全问题解决
//static void Main(string[] args)
//{
// //实例化集合对象
// ArrayList list = new ArrayList();
// //添加对象元素
// Student objStu1 = new Student() { Age = 20, StuId = 1001, StuName = "小张" };
// Student objStu2 = new Student() { Age = 22, StuId = 1003, StuName = "小李" };
// Student objStu3 = new Student() { Age = 22, StuId = 1002, StuName = "小王" };
// list.Add(objStu1);
// list.Add(objStu2);
// list.Add(objStu3);
// //添加教师对象
// list.Add(new Teacher() { Name = "王老师", Age = 28 });
// //遍历元素
// for (int i = 0; i < list.Count; i )
// {
// if (list[i] is Student)
// {
// Student obj = (Student)list[i];
// Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", obj.StuName, obj.Age, obj.StuId);
// }
// else if (list[i] is Teacher)
// {
// Teacher obj = (Teacher)list[i];
// Console.WriteLine("姓名:{0} 年龄:{1} ", obj.Name, obj.Age, obj.Age);
// }
// }
// Console.ReadLine();
//}
#endregion
#region 示例6:List集合常用方法
//static void Main(string[] args)
//{
// //实例化List<T>集合对象
// List<Student> list = new List<Student>();
// //添加对象元素
// Student objStu1 = new Student() { Age = 20, StuId = 1001, StuName = "小张" };
// Student objStu2 = new Student() { Age = 22, StuId = 1003, StuName = "小李" };
// Student objStu3 = new Student() { Age = 22, StuId = 1002, StuName = "小王" };
// list.Add(objStu1);
// list.Add(objStu2);
// list.Add(objStu3);
// Student objStu4 = new Student() { Age = 25, StuId = 1004, StuName = "小秦" };
// //在指定位置插入元素,指定位置元素会相应后移
// list.Insert(1, objStu4);
// //遍历元素,取出元素时不需要强制转换
// for (int i = 0; i < list.Count; i )
// {
// Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", list[i].StuName, list[i].Age, list[i].StuId);
// }
// Console.ReadLine();
//}
#endregion
#region 示例7:泛型集合对元素类型的约束
//static void Main(string[] args)
//{
// //实例化List<T>集合对象
// List<Student> list = new List<Student>();
// //添加对象元素
// Student objStu1 = new Student() { Age = 20, StuId = 1001, StuName = "小张" };
// Student objStu2 = new Student() { Age = 22, StuId = 1003, StuName = "小李" };
// Student objStu3 = new Student() { Age = 22, StuId = 1002, StuName = "小王" };
// list.Add(objStu1);
// list.Add(objStu2);
// list.Add(objStu3);
// Teacher objTeacher=new Teacher() { Name = "王老师", Age = 28 };
// list.Add(objTeacher);
// //遍历元素,取出元素时不需要强制转换
// for (int i = 0; i < list.Count; i )
// {
// Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", list[i].StuName, list[i].Age, list[i].StuId);
// }
// Console.ReadLine();
//}
#endregion
#region 示例8:集合初始化器的使用
//static void Main(string[] args)
//{
// //集合初始化器:使用基本数据类型元素
// List<string> nameList = new List<string>()
// {
// "小张","小李","小秦"
// };
// //集合初始化器和对象初始化器共同使用
// List<Student> stuList = new List<Student>()
// {
// new Student() { Age = 20, StuId = 1001, StuName = "小张" },
// new Student() { Age = 22, StuId = 1003, StuName = "小李" },
// new Student() { Age = 22, StuId = 1002, StuName = "小王" }
// };
// foreach (string item in nameList)
// {
// Console.WriteLine(item);
// }
// foreach (Student stu in stuList)
// {
// Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", stu.StuName, stu.Age, stu.StuId);
// }
// Console.ReadLine();
//}
#endregion
#region 示例9:Dictionary<K,V>的使用
//static void Main(string[] args)
//{
// //实例化集合对象
// Dictionary<string, Student> stus = new Dictionary<string, Student>();
// //添加对象元素
// Student objStu1 = new Student() { Age = 20, StuId = 1001, StuName = "小张" };
// Student objStu2 = new Student() { Age = 22, StuId = 1003, StuName = "小李" };
// Student objStu3 = new Student() { Age = 22, StuId = 1002, StuName = "小王" };
// stus.Add("小张", objStu1);
// stus.Add("小李", objStu2);
// Console.WriteLine("小李的年龄:{0}", stus["小李"].Age);//通过key查找value
// stus.Add("小王", objStu3);
// Console.WriteLine("小李的年龄:{0}", stus["小李"].Age);
// //遍历key
// foreach (string item in stus.Keys)
// {
// Console.WriteLine(item);
// }
// //遍历values
// foreach (Student stu in stus.Values)
// {
// Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", stu.StuName, stu.Age, stu.StuId);
// }
// Console.ReadLine();
//}
#endregion
#region 示例10:基本数据类型元素的排序
//static void Main(string[] args)
//{
// //集合初始化器:使用基本数据类型元素
// List<string> nameList = new List<string>()
// {
// "小张","小李","小秦","小东","小唐","小白"
// };
// Console.WriteLine ("-------------字符串默认排序--------------");
// foreach (string item in nameList)
// {
// Console.WriteLine(item);
// }
// nameList.Sort();//使用Sort()方法对集合中元素排序(默认升序)
// Console.WriteLine("-------------字符串排序后--------------");
// foreach (string item in nameList)
// {
// Console.WriteLine(item);
// }
// nameList.Reverse();//集合元素反转
// Console.WriteLine("-------------元素反转后--------------");
// foreach (string item in nameList)
// {
// Console.WriteLine(item);
// }
// Console.ReadLine();
//}
//static void Main(string[] args)
//{
// //集合初始化器:使用基本数据类型元素
// List<int> ageList = new List<int>()
// {
// 20,27,21,26,28,30
// };
// Console.WriteLine("-------------整数元素默认排序--------------");
// foreach (int item in ageList)
// {
// Console.WriteLine(item);
// }
// ageList.Sort();//使用Sort()方法对集合中元素排序(默认升序)
// Console.WriteLine("-------------整数元素排序后--------------");
// foreach (int item in ageList)
// {
// Console.WriteLine(item);
// }
// ageList.Reverse();//集合元素反转
// Console.WriteLine("-------------元素反转后--------------");
// foreach (int item in ageList)
// {
// Console.WriteLine(item);
// }
// Console.ReadLine();
//}
#endregion
#region 示例11:对象类型元素排序的问题
//static void Main(string[] args)
//{
// //实例化List<T>集合对象
// List<Student> list = new List<Student>();
// //添加对象元素
// Student objStu1 = new Student() { Age = 20, StuId = 1001, StuName = "小张" };
// Student objStu2 = new Student() { Age = 22, StuId = 1003, StuName = "小李" };
// Student objStu3 = new Student() { Age = 22, StuId = 1002, StuName = "小王" };
// list.Add(objStu1);
// list.Add(objStu2);
// list.Add(objStu3);
// list.Sort();//将对象元素排序
// for (int i = 0; i < list.Count; i )
// {
// Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", list[i].StuName, list[i].Age, list[i].StuId);
// }
// Console.ReadLine();
//}
#endregion
#region 示例12:实现IComparable接口完成对象默认排序
//static void Main(string[] args)
//{
// //实例化List<T>集合对象
// List<Student> list = new List<Student>();
// //添加对象元素
// Student objStu1 = new Student() { Age = 20, StuId = 1001, StuName = "小张" };
// Student objStu2 = new Student() { Age = 22, StuId = 1003, StuName = "小李" };
// Student objStu3 = new Student() { Age = 22, StuId = 1002, StuName = "小王" };
// list.Add(objStu1);
// list.Add(objStu2);
// list.Add(objStu3);
// list.Sort();//将对象元素排序(排序对象已经实现IComparable泛型接口)
// for (int i = 0; i < list.Count; i )
// {
// Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", list[i].StuName, list[i].Age, list[i].StuId);
// }
// Console.ReadLine();
//}
#endregion
#region 示例12:实现IComparable接口完成对象默认排序
//static void Main(string[] args)
//{
// //实例化List<T>集合对象
// List<Student> list = new List<Student>();
// //添加对象元素
// Student objStu1 = new Student() { Age = 20, StuId = 1001, StuName = "小张" };
// Student objStu2 = new Student() { Age = 22, StuId = 1003, StuName = "小李" };
// Student objStu3 = new Student() { Age = 22, StuId = 1002, StuName = "小王" };
// list.Add(objStu1);
// list.Add(objStu2);
// list.Add(objStu3);
// list.Sort();//按照学号升序排列
// for (int i = 0; i < list.Count; i )
// {
// Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", list[i].StuName, list[i].Age, list[i].StuId);
// }
// Console.ReadLine();
//}
#endregion
#region 示例13:实现IComparable接口完成对象按姓名排序
//static void Main(string[] args)
//{
// //实例化List<T>集合对象
// List<Student> list = new List<Student>();
// //添加对象元素
// Student objStu1 = new Student() { Age = 20, StuId = 1001, StuName = "小张" };
// Student objStu2 = new Student() { Age = 22, StuId = 1003, StuName = "小李" };
// Student objStu3 = new Student() { Age = 22, StuId = 1002, StuName = "小王" };
// list.Add(objStu1);
// list.Add(objStu2);
// list.Add(objStu3);
// list.Sort();//按照姓名降序排列
// for (int i = 0; i < list.Count; i )
// {
// Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", list[i].StuName, list[i].Age, list[i].StuId);
// }
// Console.ReadLine();
//}
#endregion
#region 示例14:使用比较器ICompare<T>实现动态排序
//static void Main(string[] args)
//{
// //实例化List<T>集合对象
// List<Student> list = new List<Student>();
// //添加对象元素
// Student objStu1 = new Student() { Age = 20, StuId = 1001, StuName = "小张" };
// Student objStu2 = new Student() { Age = 22, StuId = 1003, StuName = "小李" };
// Student objStu3 = new Student() { Age = 22, StuId = 1002, StuName = "小王" };
// list.Add(objStu1);
// list.Add(objStu2);
// list.Add(objStu3);
// //默认排序
// Console.WriteLine("------------默认排序------------");
// for (int i = 0; i < list.Count; i )
// {
// Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", list[i].StuName, list[i].Age, list[i].StuId);
// }
// //年龄降序排序
// Console.WriteLine("------------年龄降序排序------------");
// list.Sort(new AgeDESC());
// for (int i = 0; i < list.Count; i )
// {
// Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", list[i].StuName, list[i].Age, list[i].StuId);
// }
// //姓名升排序
// Console.WriteLine("------------姓名升序排序------------");
// list.Sort(new NameASC());
// for (int i = 0; i < list.Count; i )
// {
// Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", list[i].StuName, list[i].Age, list[i].StuId);
// }
// Console.ReadLine();
//}
#endregion
#region 示例15:使用LINQ排序【后面VIP课程中详细讲解】
//static void Main(string[] args)
//{
// //实例化List<T>集合对象
// List<Student> list = new List<Student>();
// //添加对象元素
// Student objStu1 = new Student() { Age = 20, StuId = 1001, StuName = "小张" };
// Student objStu2 = new Student() { Age = 25, StuId = 1003, StuName = "小李" };
// Student objStu3 = new Student() { Age = 22, StuId = 1002, StuName = "小王" };
// list.Add(objStu1);
// list.Add(objStu2);
// list.Add(objStu3);
// //默认排序
// Console.WriteLine("------------默认排序------------");
// for (int i = 0; i < list.Count; i )
// {
// Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", list[i].StuName, list[i].Age, list[i].StuId);
// }
// //年龄降序排序
// Console.WriteLine("------------年龄降序排序------------");
// List<Student> stuList = list.OrderByDescending(s => s.Age).ToList();
// for (int i = 0; i < stuList.Count; i )
// {
// Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", stuList[i].StuName, stuList[i].Age, stuList[i].StuId);
// }
// //姓名升序排序
// Console.WriteLine("------------姓名升序排序------------");
// stuList = list.OrderBy(s => s.StuName).ToList();
// for (int i = 0; i < list.Count; i )
// {
// Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", stuList[i].StuName, stuList[i].Age, stuList[i].StuId);
// }
// Console.ReadLine();
//}
#endregion
}
}
标签:
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论