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方法添加类目到集合中,下面的示例中有文字说明,所以请仔细查看。

List带泛型的class java list泛型集合_System

List带泛型的class java list泛型集合_List带泛型的class java_02

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

List带泛型的class java list泛型集合_System_03

复杂类型的列表集合

让我们看一个复杂类型的列表集合。下列代码中,创建一个Employee类,然后创建一些employee对象,再创建一个Employee类型的集合,将所有employee对象存储进集合中。最后,用List<T>泛型集合类提供的函数方法进行不同类型的操作。

List带泛型的class java list泛型集合_System

List带泛型的class java list泛型集合_List带泛型的class java_02

1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ListCollection
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             Employee emp1 = new Employee()
13             {
14                 ID = 101,
15                 Name = "Pranaya",
16                 Gender = "Male",
17                 Salary = 5000
18             };
19             Employee emp2 = new Employee
20             {
21                 ID = 102,
22                 Name = "Priyanka",
23                 Gender = "Female",
24                 Salary = 7000
25             };
26             Employee emp3 = new Employee()
27             {
28                 ID = 103,
29                 Name = "Anurag",
30                 Gender = "Male",
31                 Salary = 5500
32             };
33             Employee emp4 = new Employee()
34             {
35                 ID = 104,
36                 Name = "Sambit",
37                 Gender = "Male",
38                 Salary = 6500
39             };
40             List<Employee> listEmployees = new List<Employee>();
41             listEmployees.Add(emp1);
42             listEmployees.Add(emp2);
43             listEmployees.Add(emp3);
44 
45             Employee emp = listEmployees[0];
46             Console.WriteLine("Retrive the First employee by index.");
47             Console.WriteLine( "ID = {0},Name = {1},Gender = {2},Salary = {3}",emp.ID,emp.Name,emp.Gender,emp.Salary);
48             Console.WriteLine();
49         }
50     }
51 
52     public class Employee
53     { 
54         public int ID { get; set; }
55         public string Name { get; set; }
56         public string Gender { get; set; }
57         public int Salary { get; set; }
58     }
59 }

第一部分

List带泛型的class java list泛型集合_ci_06

 

List带泛型的class java list泛型集合_System

List带泛型的class java list泛型集合_List带泛型的class java_02

1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ListCollection
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             Employee emp1 = new Employee()
13             {
14                 ID = 101,
15                 Name = "Pranaya",
16                 Gender = "Male",
17                 Salary = 5000
18             };
19             Employee emp2 = new Employee
20             {
21                 ID = 102,
22                 Name = "Priyanka",
23                 Gender = "Female",
24                 Salary = 7000
25             };
26             Employee emp3 = new Employee()
27             {
28                 ID = 103,
29                 Name = "Anurag",
30                 Gender = "Male",
31                 Salary = 5500
32             };
33             Employee emp4 = new Employee()
34             {
35                 ID = 104,
36                 Name = "Sambit",
37                 Gender = "Male",
38                 Salary = 6500
39             };
40             List<Employee> listEmployees = new List<Employee>();
41             listEmployees.Add(emp1);
42             listEmployees.Add(emp2);
43             listEmployees.Add(emp3);
44 
45             Employee emp = listEmployees[0];
46             Console.WriteLine("Retrive the First employee by index.");
47             Console.WriteLine( "ID = {0},Name = {1},Gender = {2},Salary = {3}",emp.ID,emp.Name,emp.Gender,emp.Salary);
48             Console.WriteLine();
49 
50             Console.WriteLine("Retriving the first using for loop");
51             for (int i = 0; i < listEmployees.Count; i++)
52             {
53                 Employee employee = listEmployees[i];
54                 Console.WriteLine("ID = {0},Name = {1} , Gender = {2}, Salary = {3}",employee.ID,employee.Name,employee.Gender,employee.Salary);
55             }
56             Console.WriteLine();
57 
58             Console.WriteLine("Retriving the list using foreach loop");
59             foreach (Employee e in listEmployees)
60             {
61                 Console.WriteLine("ID = {0}, Name = {1}, Gender = {2}, Salary = {3}",
62                                 e.ID,e.Name,e.Gender,e.Salary);
63             }
64             Console.WriteLine();
65 
66             //在位置1插入一个Employee
67             listEmployees.Insert(1,emp4);
68             Console.WriteLine("Retriving the list after inserting new employee in index 1");
69             foreach (Employee e   in listEmployees)
70             {
71                 Console.WriteLine("ID = {0}, Name = {1}, Gender = {2}, Salary = {3}",
72                                 e.ID,e.Name,e.Gender,e.Salary);
73             }
74             Console.WriteLine();
75 
76             Console.WriteLine("Index of emp3 object in the list = " +
77                                 listEmployees.IndexOf(emp3));
78             Console.ReadKey();
79         }
80     }
81 
82     public class Employee
83     { 
84         public int ID { get; set; }
85         public string Name { get; set; }
86         public string Gender { get; set; }
87         public int Salary { get; set; }
88     }
89 }


List带泛型的class java list泛型集合_List带泛型的class java_09

 

 注:所有 的泛型类在C#中都是强类型。那意味着如果我们创建 了Employee类型列表集合,那我们只能添加Employee类型的对象到列表中。如果我们尝试添加一个不同类型的对象,编译器会报错。