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 ...
随机推荐
- HttpContext.Current 的缺陷
了解ASP.NET的开发人员都知道它有个非常强大的对象 HttpContext,而且为了方便,ASP.NET还为它提供了一个静态属性HttpContext.Current来访问它,今天的博客打算就从H ...
- java中Date的getTime() 方法奇葩问题
今天遇到了一个奇葩问题: 从数据库中读取了3个Date类型的数据: DATE1:2015-03-12 12:10:42 DATE2:2015-03-12 12:04:40 DATE3:2015-03- ...
- CCF真题之相反数
201403-1 问题描述 有 N 个非零且各不相同的整数.请你编一个程序求出它们中有多少对相反数(a 和 -a 为一对相反数). 输入格式 第一行包含一个正整数 N.(1 ≤ N ≤ 500). ...
- logstash5.x改变
5.x版本 logstash中 elasticsearch插件的workers,无法配置大于1,会提示 This plugin uses the shared and doesn't need thi ...
- 【py网页】urlopen的补充,完美
urllib 是 python 自带的一个抓取网页信息一个接口,他最主要的方法是 urlopen(),是基于 python 的 open() 方法的.下面是主要说明: 1 urllib.urlopen ...
- 【crunch bang】调整窗口大小
在终端下, <super> + 上箭头 == 向上调整大小 <super> + 下箭头(左.右)
- OpenStack 计算节点删除
前提 计算节点中一个僵尸计算节点存在,而里面的CPU数目在总物理CPU中,导致认为当前能创建实例.而实际没有这么多资源. 其中node-11为僵尸节点. 原因 删除计算节点不能直接格式化该服务器,否则 ...
- 去除冗余 – 精简您的CSS样式代码
讲讲常见的一些没有必要使用CSS代码情况,而这些不起作用可以去掉的CSS代码可能是我们经常忽视的.越是对CSS理解不够,越容易出现这些问题. 二.一些常见不必要CSS样式 1.与默认CSS样式一致 我 ...
- Android BlueDroid(一):BlueDroid概述 【转】
转自:http://blog.csdn.net/xubin341719/article/details/40378205 版权声明:本文为博主原创文章,未经博主允许不得转载. 关键词:bluedroi ...
- java中IO流操作的标准异常类
package 加入异常处理的字节流操作; import java.io.FileNotFoundException; import java.io.FileOutputStream; import ...