List<T>
List<FormEntity> formEntity = new List<FormEntity> (){new FormEntity{ IsFile = true,Name = "Media",Value = MapPath("1.png")}};
Utils.HttpPostForm(url,new List<FormEntity>(){new FormEntity{ IsFile = true,Name = "Media",Value = MapPath("1.png")}});
List的一般用法
所属命名空间: System.Collections.Generic
public class List<T>:IList<T>,Icollection<T>,IEnumerable<T>,IList,Icollection,Ienumerable
List<T>是ArrayList类的泛型等效类,该类使用大小可按需动态增加的数组实现IList<T>泛型接口
(1)声明 List<T>mlist = new List<T>();
eg: string[] Arr = {"a","b","c"};
List<string> mlist = new List<string>(Arr);
(2)添加一个元素 List.Add(T item)
eg: mlist.Add("d");
(3)添加集合元素
eg: string[] Arr2 ={"f","g"."h"};
mlist.AddRange(Arr2);
(4)在index位置添加一个元素 Insert(int index,T item)
eg: mlist.Insert(1,"p");
(5)遍历List中元素
foreach(T element in mlist) T的类型与mlist声明时一样
{
Console.WriteLine(element);
}
eg:
foreach(string s in mlist)
{
Console.WriteLine(s);
}
(6)删除元素
List.Remove(T item) 删除一个值
eg: mlist.Remove("a");
List.RemoveAt(int index);删除下标为index的元素
eg: mlist.RemoveAt(0);
List.RemoveRange(int index,int count); 下标index开始,删除count个元素
eg:mlist.RemoveRange(3,2);
(7)判断某个元素是否在该List中
List.Contains(T item) 返回true或false
eg:
if(mlist.Contains"("g"))
Console.WriteLine("g存在列表中");
else
mlist.Add("g");
(8)给List里面元素排序 List.Sort() 默认是元素每一个字母按升序
eg: mlist.Sort();
(9)给List里面元素顺序反转 List.Reverse() 可以与List.Sort()配合使用
(10)List清空 List.Clear()
eg: mlist.Clear();
(11)获得List中元素数目 List.Count() 返回int值
eg: mlist.count();
List进阶,强大方法
(1)List.FindAll方法:检索与指定谓词所定义的条件相匹配的所有元素
class program
{
static void Main(stirng[] args)
{
student stu = new student();
stu.Name="arron";
List<student> students= new List<student>();
students.Add(stu);
students.Add(new student("candy"));
FindName myname = new FindName("arron");
foreach(student s in students.FindAll(new Predicate<student>(myname.IsName)))
{ Console.WriteLine(s);}
}
public class student
{
public string Name{get;set;}
public student(){}
public override string ToString()
{
return string.Format("姓名:{0}",Name);
}
}
public class FindName
{
private string _name;
public FindName(string Name)
{ this._name=Name;}
public bool IsName(student s)
{ return (s.Name==_name)?true:false;}
}
(2)List.Find方法 搜索与指定谓词所定义的条件相匹配的元素,并返回整个List中的第一个匹配元素
eg:
//Predicate是对方法的委托,如果传递给它的对象与委托定义的条件匹配,则该方法返回true,当前List的元素
被逐个传递给Predicate委托,并在List中间前移动,从第一个元素开始,到最后一个元素结束,当找到匹配项
时处理停止
第一种方法 委托给拉姆达表达式:
eg:
string listFind = mlist.Find(name=>
{
if(name.length>3)
return true;
return false;
});
第二种方法 委托给一个函数
eg:
public bool ListFind(string name)
{
if (name.Length > 3)
{
return true;
}
return false;
}
这两种方法的结果是一样的
(3) List.FindLast方法 public T FindLast(Predicate<T> match);确定是否 List 中的每个元素都与指定的谓词所定义的条件相匹配。用法与List.Find相同。
(4) List.TrueForAll方法: 确定是否 List 中的每个元素都与指定的谓词所定义的条件相匹配。
public bool TrueForAll(Predicate<T> match);
(5) List.Take(n): 获得前n行 返回值为IEnumetable<T>,T的类型与List<T>的类型一样
E.g.:
IEnumerable<string> takeList= mList.Take(5);
foreach (string s in takeList)
{
Console.WriteLine("element in takeList: " + s);
}
这时takeList存放的元素就是mList中的前5个
(6) List.Where方法:检索与指定谓词所定义的条件相匹配的所有元素。跟List.FindAll方法类似。
E.g.:
IEnumerable<string> whereList = mList.Where(name =>
{
if (name.Length > 3)
{
return true;
}
else
{
return false;
}
});
foreach (string s in subList)
{
Console.WriteLine("element in subList: "+s);
}
这时subList存储的就是所有长度大于3的元素
(7)List.RemoveAll方法:移除与指定的谓词所定义的条件相匹配的所有元素。
public int RemoveAll(Predicate<T> match);
E.g.:
mList.RemoveAll(name =>
{
if (name.Length > 3)
{
return true;
}
else
{
return false;
}
});
foreach (string s in mList)
{
Console.WriteLine("element in mList: " + s);
}
这时mList存储的就是移除长度大于3之后的元素
List<T>的更多相关文章
- FastJSON 简介及其Map/JSON/String 互转
在日志解析,前后端数据传输交互中,经常会遇到 String 与 map.json.xml 等格式相互转换与解析的场景,其中 json 基本成为了跨语言.跨前后端的事实上的标准数据交互格式.应该来说各个 ...
- JSON数组形式字符串转换为List<Map<String,String>>的8种方法
package com.zkn.newlearn.json; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArr ...
- JSON数组形式字符串转换为List<Map<String,String>>的几种方法
package com.zkn.newlearn.json; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArr ...
- 011-JSON、JSONObject、JSONArray使用、JSON数组形式字符串转换为List<Map<String,String>>的8种方法
一.JSON数据格式 1.1.常用JSON数据格式 1.对象方式:JSONObject的数据是用 { } 来表示的, 例如: { "id" : "123", & ...
- 入门:Java Map<String,String>遍历及修改
重点:在使用Map时注意key-value,key用于检索value的内容. 在正常情况下,可以不允许重复:在java中分为2中情况,一是内存地址重复,另一个是不同的地址但内容相等. 在使用Map是一 ...
- alibaba fastjson List<Map<String, String>>2Str
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map; impo ...
- getParameterMap()的返回值为Map<String, String[]>,从其中取得请求参数转为Map<String, String>的方法如下:
直接遍历报错:[Ljava.lang.String;@44739f3f Map<String, String> tempMap = new HashMap<String, Strin ...
- JAVA/Android Map与String的转换方法
在Android开发中 Map与String的转换在,在一些需求中经常用到,使用net.sf.json.JSONObject.fromObject可以方便的将string转为Map.但需要导入jar包 ...
- 使用STL map 用 string 做索引 插入删除数据
1.代码 #include <map> #include <string> #include <stdio.h> #include <vector> # ...
- POJ2503——Babelfish(map映射+string字符串)
Babelfish DescriptionYou have just moved from Waterloo to a big city. The people here speak an incom ...
随机推荐
- hdu5381 The sum of gcd
莫队算法,预处理出每个数字往后的gcd情况,每个数字的gcd只可能是他的因子,因此后面最多只可能有logn种,可以先预处理出,然后套莫队算法,复杂度O(n*sqrt(n)*log(n)). 代码 #i ...
- (转)VS2008连接TFS 2010
偶尔还是会用到,老是忘记安装的顺序,在这儿mark一下. 用VS2008连接TFS 2010, 需要按照以下顺序安装一下组件: .VS2008 Team Explorer 2008 3.Install ...
- Android Notification通知栏使用
package com.example.mynotifycation; import android.app.Activity; import android.app.Notification; im ...
- SQL Server数据库性能优化(一)之 优化SQL 语句
最近工作上基本没什么需求(好吧 不是最近是好久了,所以随便看看基础的东西来填补自己的空白) 原文出自:http://www.blogjava.net/allen-zhe/archive/2010/07 ...
- .scss写法及如何转化为.css
scss可视化工具:http://koala-app.com/index-zh.html scss讲解地址:http://www.cnblogs.com/adine/archive/2012/12/1 ...
- sql 去除列中内容的单引号
update [dbo].[历史数据补全$] set bankCardNo=replace(bankCardNo,'''','') 总共4个单引号,外边2个是字符串固有的2个,里边两个就表示是一 ...
- PAT乙级 1003. 我要通过!(20)
答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”. 得到“答案正确”的条件是: 1. ...
- SVN使用(二)
TortoiseSVN是windows平台下Subversion的免费开源客户端. 一般我们都是先讲讲服务器的配置,然后再讲客户端的使用,但是在TortoiseSVN上,却可以反过来.因为,如果你的要 ...
- Loadrunner教程读后感-VuGen
一.loadrunner协议分析 协议确定方法 二.提交表单函数的区别 (1)web_sumit_form() (2)web_sumit_data() 三.web_url和web_link 四.VuG ...
- 【翻译】了解ASP.NET MVC的HTML助手
原文:Understanding HTML Helpers in ASP.NET MVC 作 者:Shailendra Chauhan works as Software Analyst at rep ...