ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

List<T>泛型类

2022-02-23 23:34:05  阅读:150  来源: 互联网

标签:Console integerList List 列表 集合 WriteLine 泛型


List<T>示例

这篇文章中,我们会用示例讨论List<T>泛型类;List<T>是目前隶属于System.Collections.Generic名称空间的集合类。List集合类是使用范围最广的泛型集合类之一。List<T>集合类是可以用索引进行处理一种强对象列表类型。List<T>提供了很多函数,用于搜索、排列和操作列表项。

通过使用List<T>我们可以创建任何类型的集合,比如,如果我们需要,我们可以创建string列表,整数列表,甚至也可以创建用户自定义的复杂类型的列表,如customers列表,products列表等等。我们需要记住的最重要的一点是当我们向集合中添加项目时,列表集合的容量会自动扩展。

List列表集合中的方法和属性

下面是列表集合中一些有用的方法和属性

1、Add(T value) :此函数是用于向列表集合的尾部添加类目;

2、Remove(T value):此函数用于从 List<T> 中移除特定对象的第一个匹配项。。

3、RemoveAt(int index):此函数用于查找元素的索引位置,然后从列表集合中进行移除。

4、Insert(int index ,T value):此函数用于在指定索引位置插入元素。

5、Capacity:此属性用于返回存入列表集合中的能够容纳的元素数量。

List<T>实例

让我们用一个例子来了解一下上面提到的列表集合的函数和属性。所以,基本上,我们创建了一个整数类型的列表,然后用Add方法添加类目到集合中,下面的示例中有文字说明,所以请仔细查看。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace List
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             List<int> integerList = new List<int>();
13             Console.WriteLine("初始时的容量:" + integerList.Capacity);
14 
15             integerList.Add(10);
16             Console.WriteLine("加入第一个类目后的容量:" + integerList.Capacity);
17             integerList.Add(20);
18             integerList.Add(30);
19             integerList.Add(40);
20             Console.WriteLine("加入第4个类目后的容量:" + integerList.Capacity);
21             integerList.Add(50);
22             Console.WriteLine("加入第5个元素后的容量:" + integerList.Capacity);
23 
24             //用for循环打印
25             Console.WriteLine("用for循环打印列表");
26             for (int i = 0; i < integerList.Count; i++)
27             {
28                 Console.WriteLine(integerList[i] + "  ");
29             }
30             Console.WriteLine();
31 
32             //自列表中移除类目
33             integerList.Remove(30);
34             Console.WriteLine("移除值30后的列表");
35             foreach (int item in integerList)
36             {
37                 Console.WriteLine(item + "  ");
38             }
39             Console.WriteLine();
40 
41             //在列表中加入数值
42             integerList.Insert(2,25);
43             Console.WriteLine("在列表的索引位置2中加入数值25");
44             foreach (int item in integerList)
45             {
46                 Console.WriteLine(item + "  ");
47             }
48             Console.WriteLine();
49 
50             List<int> newIntegerList = new List<int>(integerList);
51             Console.WriteLine("新列表的初始容量:" + newIntegerList.Capacity);
52 
53             Console.WriteLine("打印由旧的列表创建的新列表");
54             foreach (int item in newIntegerList)
55             {
56                 Console.WriteLine(item + "  ");
57             }
58             Console.WriteLine();
59 
60             List<int> TheCapacity = new List<int>(10);
61             Console.WriteLine("指定的List列表容量是:" + TheCapacity.Capacity);
62             Console.ReadKey();
63         }
64     }
65 }
View Code

 

标签:Console,integerList,List,列表,集合,WriteLine,泛型
来源: https://www.cnblogs.com/chenlight/p/15929714.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有