C#List<string>和string[]之间的相互转换
一、LIST概述
所属命名空间:System.Collections.Generic
public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
List<T>类是 ArrayList 类的泛型等效类。该类使用大小可按需动态增加的数组实现 IList<T> 泛型接口。
泛型的好处: 它为使用c#语言编写面向对象程序增加了极大的效力和灵活性。不会强行对值类型进行装箱和拆箱,或对引用类型进行向下强制类型转换,所以性能得到提高。
二、性能注意事项:
在决定使用IList<T> 还是使用ArrayList类(两者具有类似的功能)时,记住IList<T> 类在大多数情况下执行得更好并且是类型安全的。
如果对IList<T> 类的类型 T 使用引用类型,则两个类的行为是完全相同的。但是,如果对类型 T 使用值类型,则需要考虑实现和装箱问题。
“添加到 ArrayList 中的任何引用或值类型都将隐式地向上强制转换为 Object。如果项是值类型,则必须在将其添加到列表中时进行装箱操作,在检索时进行取消装箱操作。强制转换以及装箱和取消装箱操作都会降低性能;在必须对大型集合进行循环访问的情况下,装箱和取消装箱的影响非常明显。”
三、一般用法
1、 List的基础、常用方法:
声明:
1、List<T> mList = new List<T>();
T为列表中元素类型,现在以string类型作为例子
E.g.: List<string> mList = new List<string>();
2、List<T> testList =new List<T> (IEnumerable<T> collection);
以一个集合作为参数创建List
E.g.:
string[] temArr = { "Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "Locu" };
List<string> testList = new List<string>(temArr);
添加元素:
1、 List. Add(T item) 添加一个元素
E.g.: mList.Add("John");
2、 List. AddRange(IEnumerable<T> collection) 添加一组元素
E.g.:
string[] temArr = { "Ha","Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "Locu" };
mList.AddRange(temArr);
3、Insert(int index, T item); 在index位置添加一个元素
E.g.: mList.Insert(1, "Hei");
遍历List中元素:
foreach (T element in mList) T的类型与mList声明时一样
{
Console.WriteLine(element);
}
E.g.:
foreach (string s in mList)
{
Console.WriteLine(s);
}
删除元素:
1、 List. Remove(T item) 删除一个值
E.g.: mList.Remove("Hunter");
2、 List. RemoveAt(int index); 删除下标为index的元素
E.g.: mList.RemoveAt(0);
3、 List. RemoveRange(int index, int count);
从下标index开始,删除count个元素
E.g.: mList.RemoveRange(3, 2);
判断某个元素是否在该List中:
List. Contains(T item) 返回true或false,很实用
E.g.:
if (mList.Contains("Hunter"))
{
Console.WriteLine("There is Hunter in the list");
}
else
{
mList.Add("Hunter");
Console.WriteLine("Add Hunter successfully.");
}
给List里面元素排序:
List. Sort () 默认是元素第一个字母按升序
E.g.: mList.Sort();
给List里面元素顺序反转:
List. Reverse () 可以与List. Sort ()配合使用,达到想要的效果
E.g.: mList.Sort();
List清空:List. Clear ()
E.g.: mList.Clear();
获得List中元素数目:
List. Count () 返回int值
E.g.:
int count = mList.Count();
Console.WriteLine("The num of elements in the list: " +count);
2、 List的进阶、强大方法:
举例用的List:
string[] temArr = { Ha","Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", " "Locu" };
mList.AddRange(temArr);
List.Find 方法:搜索与指定谓词所定义的条件相匹配的元素,并返回整个 List 中的第一个匹配元素。
public T Find(Predicate<T> match);
Predicate是对方法的委托,如果传递给它的对象与委托中定义的条件匹配,则该方法返回 true。当前 List 的元素被逐个传递给Predicate委托,并在 List 中向前移动,从第一个元素开始,到最后一个元素结束。当找到匹配项时处理即停止。
Predicate 可以委托给一个函数或者一个拉姆达表达式:
委托给拉姆达表达式:
E.g.:
string listFind = mList.Find(name => //name是变量,代表的是mList
{ //中元素,自己设定
if (name.Length > 3)
{
return true;
}
return false;
});
Console.WriteLine(listFind); //输出是Hunter
委托给一个函数:
E.g.:
string listFind1 = mList.Find(ListFind); //委托给ListFind函数
Console.WriteLine(listFind); //输出是Hunter
ListFind函数:
public bool ListFind(string name)
{
if (name.Length > 3)
{
return true;
}
return false;
}
这两种方法的结果是一样的。
List.FindLast 方法:搜索与指定谓词所定义的条件相匹配的元素,并返回整个 List 中的最后一个匹配元素。
public T FindLast(Predicate<T> match);
用法与List.Find相同。
List.TrueForAll方法: 确定是否 List 中的每个元素都与指定的谓词所定义的条件相匹配。
public bool TrueForAll(Predicate<T> match);
委托给拉姆达表达式:
E.g.:
bool flag = mList.TrueForAll(name =>
{
if (name.Length > 3)
{
return true;
}
else
{
return false;
}
}
);
Console.WriteLine("True for all: "+flag); //flag值为false
委托给一个函数,这里用到上面的ListFind函数:
E.g.:
bool flag = mList.TrueForAll(ListFind); //委托给ListFind函数
Console.WriteLine("True for all: "+flag); //flag值为false
这两种方法的结果是一样的。
List.FindAll方法:检索与指定谓词所定义的条件相匹配的所有元素。
public List<T> FindAll(Predicate<T> match);
E.g.:
List<string> subList = mList.FindAll(ListFind); //委托给ListFind函数
foreach (string s in subList)
{
Console.WriteLine("element in subList: "+s);
}
这时subList存储的就是所有长度大于3的元素
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个
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的元素
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> 是一个泛型链表...T表示节点元素类型 比如 List<int> intList;表示一个元素为int的链表 intList.Add(34); //添加 intList.Remove(34);//删除 intList.RemoveAt(0); //删除位于某处的元素 intList.Count; //链表长度 还有Insert,Find,FindAll,Contains等方法,也有索引方法 intList[0] = 23; 1.减少了装箱拆箱 2.便于编译时检查数据类型
List<Object> 就相当于 System.Collections命名空间里面的List
四、C#List<string>和string[]之间的相互转换
1.从System.String[]转到List<System.String>
List<System.String> List = new List<System.String>(); string[] str={"1","2","3"};
List = new List<System.String>(str);
2.从List<System.String>转到System.String[]
List<System.String> List = new List<System.String>();
List.Add("1");
List.Add("2");
List.Add("3");
System.String[] str = { };
str = List.ToArray();
3.字符串数组可以,其他有些类型像int数组等等的也是可以的。
C#List<string>和string[]之间的相互转换的更多相关文章
- C#中String 与Color之间的相互转换
C#中String 与Color之间的相互转换 ————————————宋兴柱 其实,我们平常如果要在数据库存放Color类型值的话,肯定会在数据库中建立varchar类型.对吧.但是Colo ...
- 深入理解c++中char*与wchar_t*与string以及wstring之间的相互转换 [转]
本篇文章是对c++中的char*与wchar_t*与string以及wstring之间的相互转换进行了详细的分析介绍,需要的朋友参考下. #ifndef USE_H_ #define USE_H_ # ...
- 深入理解c++中char*与wchar_t*与string以及wstring之间的相互转换
本篇文章是对c++中的char*与wchar_t*与string以及wstring之间的相互转换进行了详细的分析介绍,需要的朋友参考下-复制代码 代码如下: #ifndef USE_H_ ...
- string和double之间的相互转换(C++)
很多人都写过这个标题的文章,但本文要解决的是确保负数的string和double也可以进行转换. 代码如下: string转double double stringToDouble(string nu ...
- C# Enum Name String Description之间的相互转换
最近工作中经常用到Enum中Value.String.Description之间的相互转换,特此总结一下. 1.首先定义Enum对象 public enum Weekday { [Descriptio ...
- json和string 之间的相互转换
json和string 之间的相互转换 <script type="text/javascript"> //先认识一下js中json function showInfo ...
- java Data、String、Long三种日期类型之间的相互转换
java Data.String.Long三种日期类型之间的相互转换 // date类型转换为String类型 // formatType格式为yyyy-MM-dd HH:mm:ss// ...
- C# char[]与string之间的相互转换
string 兑换 Char[] string ss = "abcdefg"; char[] cc = ss.ToCharArray(); Char[] 转换成string str ...
- Java基础【基本数据类型包装类、int与String 之间的相互转换】
为什么会有基本类型包装类? 将基本类型数据类型封装成对象,这样的好处可以在对象中定义更多方法操作该数据. 包装类常用的操作就是用于基本数据类型与字符串之间的转换 问题:int a=100; 为什么不能 ...
随机推荐
- 安装meteor运行基本demo发生错误。
bogon:~ paul$ curl https://install.meteor.com/ | sh % Total % Received % Xferd Average Speed Time Ti ...
- css inline-block列表布局
一.使用inline-block布局 二.多列布局方法二 <html><head> <meta charset="utf-8"> <tit ...
- sql sever 创建临时表的两种方法
创建临时表 方法一: create table #临时表名( 字段1 约束条件, 字段2 约束条件, .....) ...
- 鼠标点击textarea后,在光标后追加内容
$("#insertMsg").on("click",function(){ //获取下拉选项框的值 var textFeildValue = $(" ...
- Qihoo 360 altas 实践
Qihoo 360 altas 实践 简介 Atlas是由 Qihoo 360公司Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目.它在MySQL官方推出的MySQL-Prox ...
- HDU-1035 Robot Motion 模拟问题(水题)
题目链接:https://cn.vjudge.net/problem/HDU-1035 水题 代码 #include <cstdio> #include <map> int h ...
- Linux 常用命令:系统状态篇
前言 Linux常用命令中,有些命令可以用于查看系统的状态,通过了解系统当前的状态,能够帮助我们更好地维护系统或定位问题.本文就简单介绍一下这些命令. 1. 查看系统运行时间--uptime 有时候我 ...
- Win10平台下通过VMware虚拟机安装Win7、Ubuntu、Mac
1.安装VMware14.1.1 下载地址:https://download.csdn.net/download/jasonczy/10611423 产品秘钥: CG54H-D8D0H-H8DHY-C ...
- HDU 4406 GPA
GPA Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 440664 ...
- How to test Heat (by quqi99)
作者:张华 发表于:2015-12-19版权声明:能够随意转载.转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 (http://blog.csdn.net/quqi99 ) Heat ...