c# 如何获取List中的元素,其索引大于int.MaxValue

c# 如何获取List中的元素,其索引大于int.MaxValue,第1张

C#的List为泛型集合,所属命名空间:

SystemCollectionsGeneric     

public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable

List<T>类是 ArrayList 类的泛型等效类。该类使用大小可按需动态增加的数组实现 IList<T> 泛型接口。 泛型的好处: 它为使用c#语言编写面向对象程序增加了极大的效力和灵活性。不会强行对值类型进行装箱和拆箱,或对引用类型进行向下强制类型转换,所以性能得到提高。

一般用法如下:

1、  List的基础、常用方法,声明:

List<T> mList = new List<T>();  //T为列表中元素类型,现在以string类型作为例子

List<string> mList = new List<string>(); 

List<T> testList =new List<T> (IEnumerable<T> collection);//以一个集合作为参数创建List string[] temArr = { "Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "Locu" };

List<string> testList = new List<string>(temArr);

2、添加元素:

List Add(T item);//添加一个元素

ListAdd("John");

List AddRange(IEnumerable<T> collection);//添加一组元素

string[] temArr = { "Ha","Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku",  "Locu" };

ListAddRange(temArr);

ListInsert(int index, T item);//在index位置添加一个元素

ListInsert(1, "Hei");

3、 遍历List中元素:

foreach (T element in mList)  T的类型与mList声明时一样

  {

       ConsoleWriteLine(element);

  }

foreach (string s in mList)

{

    ConsoleWriteLine(s);

}

4、删除元素:

List Remove(T item);//删除一个值

mListRemove("Hunter");

List RemoveAt(int index);//删除下标为index的元素

mListRemoveAt(0);

List RemoveRange(int index, int count);//从下标index开始,删除count个元素

mListRemoveRange(3, 2);

foreach循环中是不允许在递归中修改循环变量的。

可以换一种方式,for(int pos=0;pos<sockListCount;pos++){sockList[pos]},

即使用索引访问。

List中的get(i)方法是获取List中的第i+1个对象。因为List是从0开始的,List是有序的可重复的集合接口。

List<People> list = new ArrayList<People>();

for(int i = 0;i<listsize();i++){

People people = listget(i);

}

//当i=0时,取得是list集合中第一个元素,

//当i=1时,取得是list集合中第二个元素,

//当i=i时,取得是list集合中第i+1个元素。

一个是获取list集合中的第一个元素,第二个是获取指定索引的元素。

扩展资料

List list = new ArrayList();

listadd(xxx);

listadd(yyy);

listget(0);是获取list里面索引为0的(也就是第一个)元素

listget(i);是获取list里面索引为i的(也就是第i+1个)元素

i是一个整型的变量,比如int i=5; 那么就是取出索引为5(第6个)元素

listget(i)更常用的是在遍历的时候,比如

for(int i=0;i<listsize();i++){

Systemoutprintln(listget(i));

}

参考资料来源:

百度百科——list

以上就是关于c# 如何获取List中的元素,其索引大于int.MaxValue全部的内容,包括:c# 如何获取List中的元素,其索引大于int.MaxValue、C#里怎么得到List里其中一个元素的引用、List中的get(i)方法是获取List中的第i个对象吗等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/10153094.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存