C#基础知识之List的使用
来源:互联网 发布:互联网大数据云计算 编辑:程序博客网 时间:2024/05/16 06:38
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace UseListT{ class Program { static void Main(string[] args) { //UseForeach( ); //UseSort( ); UseFinds( ); //UseRemoves( ); Console.ReadKey(); } static void AddInsertList() { int[] ary1 = new int[] { 1, 3, 7 }; //创建两个int数组ary1和ary2备用 int[] ary2 = new int[] { 2, 4, 6 }; List<int> intLst = new List<int>( ); //创建int类型的列表,新建时为空 intLst.Add(10); //添加10到末尾,intLst为{10} intLst.Add(20); //添加20到末尾intLst为{10, 20} intLst.Insert(0, 5); //在索引为0的位置插入5,intLst为{5,10,20} intLst.Insert(1, 8); //在索引为1的位置插入8,intLst为{5,8,10,20} intLst.AddRange(ary1); //将ary1添加到末尾,intLst为{5,8,10,20,1,3,7} intLst.InsertRange(2, ary2); //在索引为2的位置插入ary2, intLst为{5,8,2,4,6,8,10,20,1,3,7} foreach (int val in intLst) { System.Console.WriteLine(val); } for (int i = 0; i < intLst.Count; i++) { System.Console.WriteLine(intLst[i]); } } static void PrintInt(int val) { System.Console.WriteLine("val = {0}", val); //打印出数值 } static void PrintLevel(int val) { if (val < 60) //小于60为D等 { System.Console.WriteLine("{0} is LEVEL {1}", val, 'D'); } else if (val <= 80) //小于80为C等 { System.Console.WriteLine("{0} is LEVEL {1}", val, 'C'); } else if (val <= 90) //小于90为B等 { System.Console.WriteLine("{0} is LEVEL {1}", val, 'B'); } else if (val <= 100) //90以上为A等 { System.Console.WriteLine("{0} is LEVEL {1}", val, 'A'); } } static void UseForeach( ) { int[] scores = { 40, 80, 95}; //创建分数 List<int> scrLst = new List<int>( ); //创建List<int>对象scrLst scrLst.AddRange(scores); //添加分数scores到scrLst scrLst.ForEach(PrintInt); scrLst.ForEach(PrintLevel); } class MyIntComparer:IComparer<int> { //重写int比较器,|x|>|y|返回正数,|x|=|y|返回0,|x|<|y|返回负数 public int Compare(int x, int y) { int x1 = Math.Abs(x); //x的绝对值x1 int y1 = Math.Abs(y); //y的绝对值y1 return x1 - y1; } } //在同一行上打印列表中的元素 static void PrintList<T>(string hint, List<T> lst) { System.Console.Write(hint + ":"); foreach (T val in lst) //遍历并打印列表中的元素 { System.Console.Write("{0} ", val); } System.Console.WriteLine(); } static void UseSort() { int[] ary = {9, 8, -11, 10, -3, 2}; //准备数据,添加到列表中 MyIntComparer mic = new MyIntComparer(); List<int> intLst = new List<int>( ); intLst.AddRange(ary); //将数据添加到List<int>中 PrintList<int>("排序前", intLst); //打印数据 intLst.Sort( ); //使用默认比较器进行排序 PrintList<int>("默认排序后", intLst); //打印数据 intLst.Clear( ); //重新准备数据 intLst.AddRange(ary); intLst.Sort(mic); //用自定义的比较器进行排序,即按绝对值排序 PrintList<int>("绝对值排序后", intLst); //打印数据 intLst.Clear( ); //重新准备数据 intLst.AddRange(ary); intLst.Sort(2, 3, mic); //对第2个开始的3个元素按绝对值排序 PrintList<int>("部分排序后", intLst); //打印数据 } //泛型Predicate<string>,判断一个字符串是否以字母A开始 static bool BeginWithA(string str) { if (string.IsNullOrEmpty(str)) //如果字符串为空,则返回false return false; char ch = str[0]; //取得第一个字符 return ch == 'A'; //如果等于A返回true,否则返回false } static void UseFinds( ) { string[] ary = { "begin", "ABC", "abc", null, "Aaha", "hehe", "Aig", "end"}; //准备数据,添加到列表中 List<string> strLst = new List<string>( ); strLst.AddRange(ary); //将数据添加到List<string>中 PrintList<string>("原数据", strLst); //打印数据 string str1 = strLst.Find(BeginWithA); //查找第一个A开始的字符串 System.Console.WriteLine("第一个字符串:{0}", str1); string str2 = strLst.FindLast(BeginWithA); //查找最后一个A开始的字符串 System.Console.WriteLine("第一个字符串:{0}", str2); List<string> allStr = strLst.FindAll(BeginWithA); //查找所有A开始的字符串 PrintList<string>("全部字符串", allStr); } static void UseRemoves( ) { //准备数据,添加到列表中 string[] ary = { "begin", "ABC", "abc", "Iaha", "hehe", "Ing", "Big", "Aig", "Amer", "end" }; List<string> strLst = new List<string>( ); strLst.AddRange(ary); //将数据添加到List<string>中 PrintList<string>("原数据", strLst); //打印数据 strLst.Remove("abc"); //移除abc PrintList<string>("第一次", strLst); strLst.RemoveAt(3); //移除索引为3的元素 PrintList<string>("第二次", strLst); strLst.RemoveAll(BeginWithA); //移除所有以A开始的字符串 PrintList<string>("第三次", strLst); strLst.RemoveRange(1, 3); //移除从索引为1开始的3个元素 PrintList<string>("第四次", strLst); strLst.Clear( ); //移除所有元素 PrintList<string>("第五次", strLst); } }}
类似于C++STL模板库中的队列,链表,堆 ,栈的使用
0 0
- C#基础知识之List的使用
- C#基础知识之使用字典
- C# List的使用
- C#基础知识之if与switcth的使用
- Python 基础知识之 list
- [C#基础知识]之ArrayList的使用方法【转载】
- List的基础知识
- C#之基础知识-----接口
- C#之基础知识01
- C#基础知识之二
- C#基础知识之三
- C#基础知识之四
- C#基础知识之五
- C#基础知识之六
- C#基础知识之七
- C#基础知识之八
- C#基础知识之九
- C#基础知识之十
- hihoCoder 1143 : 骨牌覆盖问题·一(递推,矩阵快速幂)
- Android 使用UncaughtExceptionHandler 类捕抓异常信息
- Ios开发学习笔记(一)关于更改UIButton.transform属性的部分方法
- JavaScript的瀑布流效果的详解及实现
- SpringMVC注解@Autowired和@Qualifier 自动注入[根据类型注入] @Autowired 可以对成员变量、方法以及构造函数进行注释, @Qualifier 的
- C#基础知识之List的使用
- JVM GC调优一则–增大Eden Space提高性能
- Netty
- 0027-日志
- 深度学习之图像分类模型AlexNet解读
- 电量优化之Battery Historian
- 分区表,子分区表
- 数据库-面试题(持续更新)
- Json和String的转换