转载:https://www.cnblogs.com/JK1989/p/6679523.html?utm_source=itdadao&utm_medium=referral
C#中数组、集合(ArrayList)、泛型集合List<T>、字典(dictionary<TKey,TValue>)全面对比
为什么把这4个东西放在一起来说,因为c#中的这4个对象都是用来存储数据的集合……。
首先咱们把这4个对象都声明并实例化一下:
//数组
string[] m_Str = new string[5];
//集合
ArrayList m_AList = new ArrayList();
//泛型集合
List<int> m_List = new List<int>();
//字典
Dictionary<int, string> m_Dt = new Dictionary<int, string>();
大家看看这4个对象放在一起从外观上有什么异同?
我给大家提个醒,看看他们是不是都有NEW关键字,也就是说他们都需要实例化,在说明白点他们都是引用类型(不知道引用类型为何物的也不用特别纠结,以后会专门说说这个区别)。
好了咱们继续看
//数组
string[] m_Str = new string[5];
m_Str[0] = "a";
m_Str[1] = "a";
m_Str[2] = "a";
1、声明数组时[]里面有一个数字“5”,对了这就是区别,数组在声明的时候必须要指定长度。这是因为数组在内存中是连续存储的,所以它的索引速度非常快,而且赋值与修改元素也很简单。还有一点就是数组在声明定义的时候就指定了类型,我们定义的数组的类型是string的,而且因为数组是连续的,这就导致我们想在这个数组的第一个元素和第二个元素之间插队,插进一个成员是很不方便的。这就导致了我们需要一个数据集合,这个集合可以方便的让我们对该集合的成员进行add/delete/insert的操作.这就出现了集合(ArrayList)。
2、咱们看第2个对象集合(ArrayList),它在声明的时候既没有大小,也没有类型,这说明啥?说明他的大小是动态的你可以随意的add/delete/insert 如:
//集合
ArrayList m_AList = new ArrayList();
m_AList.Add("a");
m_AList.Add(10);
m_AList.Add(true);
m_AList.RemoveAt(0);
m_AList.Insert(1, "aa");
大家可能注意到集合ArrayList中Add的成员有字符串、数值、布尔值。这就说明了一个问题,集合中的每一个成员都是Object类型的,它把具体的成员装箱到object中在加入到自己。如果要是使用集合中的成员,因为成员是object类型的,所以也要拆箱到具体的类型中后再进行操作和使用。这样就会出现问题:1、消耗性能(频繁的拆箱和装箱)2、不安全,比如说上面的集合对象的第一个成员是字符型的,你取出来后和一个数值型的变量进行运算,就会报错……。那么接下来就又出现了一个对象——泛型集合(List<T>)对象。
3、泛型集合list<T>对象,大家可能一看见这个就蒙了,反正我一开始看见是蒙了,尖括号是啥,T是啥?别慌,其实不难,尖括号就是一种语法,至于T可以理解成占位符,它可以是string、int、bool……等。咱们看看代码的用法:
//泛型集合
List<int> m_List = new List<int>();
m_List.Add(10);
m_List.Add(10);
m_List.Add(10);
m_List.RemoveAt(0);
m_List.Insert(1,12);
看起来好像和集合(ArrayList)的用法差不多,其实就是差不多了,哈哈。唯一的区别在于在声明泛型集合(List<T>)的时候需要制定里面成员的类型,上面的反省集合我们制定的类型是int的,这就是说你添加的成员必须也是int的,这会有啥好处?1.数据安全了,你添加string等非int类型的成员添加不进去(编译都过不去),这样数据就安全唯一了,2.节省的性能,不需要每一操作成员的时候都要拆箱和装箱了。
泛型集合相比集合就相对完美了……
4、最后咱们说说字典Dictionary<TKey, TValue>,大家一看又蒙了,别慌,想想刚才的泛型集合(List<T>)
一样的,尖括号还是语法,你遵循就好了。而这一次的占位符不是T了,“因为第一个是索引,第二个是具体的值内容,”索引占位符变成额TKey, TValue。就这么简单呢。
下面咱们看看语法:
//字典
Dictionary<int, string> m_Dt = new Dictionary<int, string>();
m_Dt.Add(0, "a");
m_Dt.Add(1, "b");
m_Dt.Add(0, "c");
string str=m_Dt[0];
看着用法还是比较简单的吧,拿m_Dt.Add(0, "a");这一句来说0是该字典的【索引】,【值】是字符串a。要根据字典的【索引】找到具体的【值】,语法就是m_Dt[0];这样就取到了字符串a.
大家回忆一下:因为数组的使用不方便,所以有了集合(ArrayList)。因为集合(ArrayList)的不安全和消耗性能所以有了泛型集合(list<T>).这就是这3者的关系——弥补缺点。至于字典这个因为它也是储蓄一组数据的集合,同时用到了泛型的东西,所以放在一起来说了。
好了这常见的4个储存一组数据的对象咱们就讲完了,下一章引用类型和值类型咱们见。
可爱的小达……
https://www.cnblogs.com/lgx5/p/6271098.html
一、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数组等等的也是可以的。
3、C#列表List常用属性和方法
https://blog.youkuaiyun.com/qq_40985921/article/details/84402418