List<T>用法总结
- static void Main(string[] args)
- {
- Person p1 = new Person( "aladdin" , 20 );
- Person p2 = new Person("zhao", 10);
- Person p3 = new Person("jacky", 40);
- List<Person> list = new List<Person>(4);
- list.Add(p1);
- list.Add(p2);
- list.Add(p3);
- list.TrimExcess();
1 、初始化集合器
List<int> l2 = new List<int>() { 1 ,2 ,3 ,4 ,5 };
2、 添加元素
- List<Person> lists = new List<Person>(10);
- list.AddRange( new Person[] { new Person( "aladdin" ,20) , new Person("zhao",6)});
- List<Person> mylist = new List<Person>(new Person[] { new Person( "aladdin" ,20) , new Person("zhao",6)});
3、 插入元素
- mylist.Insert( 1 , new Person( "jacky" , 88 ));
- foreach (Person p in mylist)
- {
- Console.WriteLine(p.name);
- }
4 、访问元素
- Console.WriteLine( "---------访问元素-----------");
- for (int i = 0; i < mylist.Count; i++)
- {
- Console.WriteLine(mylist[i].name);
- }
5、删除元素
- mylist.RemoveAt(0);
- List<Person> lists2 = new List<Person>(10);
- Person per1 = new Person( "aladdin" , 100 );
- Person per2 = new Person("zhao", 100);
- Person per3 = new Person("jacky", 100);
- lists2.Add(per1);
- lists2.Add(per2);
- lists2.Add(per3);
- lists2.Remove(per3);
- Console.WriteLine( "-------删除后的元素---------");
- foreach (Person per in lists2)
- {
- Console.WriteLine(per.name);
- }
6 、搜索
- List<Person> ls3 = new List<Person>(10);
- Person person1 = new Person("aladdin", 100);
- Person person2 = new Person("zhao", 100);
- Person person3 = new Person("jacky", 100);
- ls3.Add(person1);
- ls3.Add(person2);
- ls3.Add(person3);
- int index = ls3.IndexOf(person3);
- Console.WriteLine( "per3 的索引:" + index);
- int index2 = ls3.IndexOf(person3,2,1);
- Console.WriteLine(index2);
- int index3 = ls3.FindIndex(param => param.name.Equals("jacky"));
- Console.WriteLine( index3 );
- int index4 = ls3.FindLastIndex(p => p.name.Equals("aladdin"));
- Console.WriteLine(index4);
- Person ppp = ls3.Find( p => p.name.Equals("jacky")) ;
- Console.WriteLine(ppp);
- List<Person> newList = ls3.FindAll(p => p.age == 100);
- Console.WriteLine( "----------查找所有---------");
- foreach (Person p in newList)
- {
- Console.WriteLine(p.name);
- }
7 、排序
- List<Person> ls4 = new List<Person>(10);
- Person person4 = new Person("aladdin", 100);
- Person person5 = new Person("zhao", 33);
- Person person6 = new Person("jacky", 44);
- ls4.Add(person4);
- ls4.Add(person5);
- ls4.Add(person6);
- ls4.Sort(MyComparFunc);
- Console.WriteLine( "------排序后的-------");
- foreach (Person p in ls4)
- {
- Console.WriteLine(p.name+ p.age );
- }
- Console.WriteLine( "--------颠倒循序--------");
- ls4.Reverse();
- foreach (Person p in ls4)
- {
- Console.WriteLine(p.name+ p.age);
- }
8 、类型转换
- List<Racer> ls5 = ls4.ConvertAll<Racer>((input) => new Racer(input.name)) ;
- Console.WriteLine( "-----------转换后的玩意--------");
- foreach (Racer r in ls5)
- {
- Console.WriteLine(r.name);
- }
9、 只读集合
- ReadOnlyCollection<Racer> persss = ls5.AsReadOnly();
- Console.WriteLine("输出只读集合");
- foreach (Racer r in persss)
- {
- Console.WriteLine(r.name);
- }
- Console.ReadLine();