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 ...
随机推荐
- /Users/alamps/AndroidStudioProjects/Demo10ScrollView
.define xml <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" andr ...
- 夺命雷公狗---node.js---20之项目的构建在node+express+mongo的博客项目5mongodb在项目中实现添加数据
我们上一步就引入了mongodb了,那么下一步就要开始写添加数据了,不过有个前提是先将表单的数据处理好: 最基本的这部现在已经成功了,因为最基本的这步就是先将表单处的提交方式和提交地址给处理好,这里和 ...
- beta阶段事后诸葛亮会议
项目名:约跑 组名:nice! 组长:李权 组员: 韩媛媛 于淼 刘芳芳 宫丽君 Beta Review会议 时间:2016.11.15 地点:冬华楼一楼大厅 会议内容: 约跑APP的Beta Rev ...
- Fury观后感
刚看完,淋雨汽车回来的,电影很精彩.前期略慢热(我还去了躺厕所),军人的黑色幽默,冷酷的军旅生活作为基调.内容我就不ao述了,新兵蛋诺曼的经历是这部电影的为主线(也有人说诺曼是观众的代入点,准确来说他 ...
- Compress a Folder/Directory via Perl5
Compress a Folder/Directory via Perl5 tested in Windows, Mac OS X, Ubuntu16.04 #!/usr/bin/perl #压缩指定 ...
- zabbix源码安装
Zabbix通过C/S模式采集数据,通过B/S模式在web端展示和配置. 被监控端:主机通过安装agent方式采集数据,网络设备通过SNMP方式采集数据 Server端:通过收集SNMP和agent发 ...
- javaWeb 使用jsp标签进行防盗链
/** * 1.新建类继承SimpleTagSupport * 新建2个属性, 添加对应的set方法 * 覆盖doTag()方法 */ import java.io.IOException; impo ...
- 深入研究java.lang.Runtime类【转】
转自:http://blog.csdn.net/lastsweetop/article/details/3961911 目录(?)[-] javalang 类 Runtime getRuntime e ...
- CSS修改方法
1.在IE中,大部分情况下默认margin = 0px padding = 0px,但在Chrome中需要写明 在css.css文件开头加上(要加在最上面) html,body,ul,ol,li,ta ...
- ServiceStack.Redis常用操作 - 事务、并发锁_转
一.事务 使用IRedisClient执行事务示例: using (IRedisClient RClient = prcm.GetClient()) { RClient.Add("key&q ...