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 ...
随机推荐
- .NET: 防止多个应用程序同时开
用到了Mutex这个类,直接看代码~ using System; using System.Collections.Generic; using System.Linq; using System.W ...
- java 8种基本数据类型
数值型--> 整 型:int,short,long,byte 浮点型:double,float 字符型-->char 布尔型-->boolean
- linux第9天 UDP
今天学了一点UDP知识,还是IP协议.都不是重点,重点是socket服务器框架 不过还是把今天学的东西,先罗列出来,将来复习的时候方便 q UDP报文可能会丢失.重复 q UDP报文可能会乱序 q ...
- 编译php时出现xsl错误的解决方法
是因为系统没安装一个叫 libxslt-devel 的包, 安装上就好了. 附编译php时的常见错误: http://www.myhack58.com/Article/sort099/sort0102 ...
- java 网络编程(一)---基础知识和概念了解
java 为用户提供了十分完善的网络功能: 1. 获取网络上的各种资源(URL) 2. 与服务器建立连接和通信(ServerSocket和Socket) 3. 无连接传递本地数据(DatagramSo ...
- 试用版SQL Server 2008 R2 提示评估期已过
解决SQL Server 2008提示评估期已过第一步:进入SQL2008配置工具中的安装中心第二步:再进入维护界面,选择版本升级第三步:进入产品密钥,输入密钥第四步:一直点下一步,直到升级完毕.SQ ...
- django view使用学习记录
判断用户是否登录 request.user.is_authenticated()auth.authenticate(username=username, password=password)
- 在 mysql 中利用 Duplicate key, 一句话实现存在的更新不存在插入功能
mysql 中可以用一个sql命令实现在插入时,如果发现唯一索引重复的记录则自动改为更新语句, 语句如下: '; 注意,radcheck 表中 username 和 attribute 列是个组合的唯 ...
- 161125、Java网络编程之统一资源定位符URL
统一资源定位符URL(Uniform Resource Locator)是www客户机访问Internet时用来标识资源的名字和地址.超文本链路由统一资源定位符URL维持.URL的格式是: <M ...
- 2014.12.05(解决eclipse的adb打不开)
一.问题如下图所示 The connection to adb is down, and a severe error has occured.You must restart adb and Ecl ...