List<T>用法总结【转】
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();
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)});
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);
- }
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);
- }
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);
- }
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);
- }
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);
- }
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);
- }
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();
List<T>用法总结【转】的更多相关文章
- EditText 基本用法
title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...
- jquery插件的用法之cookie 插件
一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...
- Java中的Socket的用法
Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...
- [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法
一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...
- python enumerate 用法
A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...
- [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结
本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...
- 【JavaScript】innerHTML、innerText和outerHTML的用法区别
用法: <div id="test"> <span style="color:red">test1</span> tes ...
- chattr用法
[root@localhost tmp]# umask 0022 一.chattr用法 1.创建空文件attrtest,然后删除,提示无法删除,因为有隐藏文件 [root@localhost tmp] ...
- 萌新笔记——vim命令“=”、“d”、“y”的用法(结合光标移动命令,一些场合会非常方便)
vim有许多命令,网上搜有一堆贴子.文章列举出各种功能的命令. 对于"="."d"."y",我在无意中发现了它们所具有的相同的一些用法,先举 ...
- [转]thinkphp 模板显示display和assign的用法
thinkphp 模板显示display和assign的用法 $this->assign('name',$value); //在 Action 类里面使用 assign 方法对模板变量赋值,无论 ...
随机推荐
- Word Search 解答
Question Given a 2D board and a word, find if the word exists in the grid. The word can be construct ...
- House Robber 解答
Question You are a professional robber planning to rob houses along a street. Each house has a certa ...
- Android ScrollView用法
Android ScrollView用法 今天试着使用了一下Android的滚轮,以下是一个小小的测试,读取测试文件,主要是使用scrollTo函数和getScrollY(),程序点击BUTTON按钮 ...
- Alice and Bob(mutiset容器)
Alice and Bob Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- linux C读取数据库
上次我们已经共同学习了在Linux下C连接数据库,下面一起学习用C语言来操作数据库. 1,首先要打开mysql的服务 [root@bogon ~]# service mysqld statusmysq ...
- strcpy与memcpy的区别
strcpy和memcpy的区别 strcpy和memcpy都是标准C库函数,它们有下面的特点. strcpy提供了字符串的复制.即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制 ...
- 【转换模型+扫描线】【UVA1398】Meteor
The famous Korean internet company nhn has provided an internet-based photo service which allows The ...
- 【二分答案】 【POJ3497】 【Northwestern Europe 2007】 Assemble 组装电脑
Assemble Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3171 Accepted: 1013 Descript ...
- JS局部打印两种方法
所有浏览器都可以 <html> <head title=""> <title>测试打印</title> <style medi ...
- C# Hashtable 使用说明 以及 Hashtable和HashMap的区别
一,哈希表(Hashtable)简述 在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似key/value的键值对,其 ...