List<> and ArrayList
List<> and ArrayList Class Diagrams
Using the Bit Complement of the BinarySearch() Result
Demonstrating FindAll() and Its Predicate Parameter
Using the Bit Complement of the BinarySearch() Result
代码
1using System;
2using System.Collections.Generic;
3class Program
4{
5 static void Main()
6 {
7 List<string> list = new List<string>();
8 int search;
9
10 list.Add("public");
11 list.Add("protected");
12 list.Add("private");
13
14 list.Sort();
15
16 search = list.BinarySearch("protected internal");
17 if (search < 0)
18 {
19 /**//*The bitwise complement (~) of this value is
20 * the index of the next element larger than the
21 * element being sought, or the total element count
22 * if there is no greater value. This provides a
23 * convenient means to insert new values into the
24 * list at the specific location so as to maintain sorting */
25 list.Insert(~search, "protected internal");
26 }
27
28 foreach (string accessModifier in list)
29 {
30 Console.WriteLine(accessModifier);
31 }
32 Console.ReadKey();
33 }
34}
1using System;
2using System.Collections.Generic;
3class Program
4{
5 static void Main()
6 {
7 List<string> list = new List<string>();
8 int search;
9
10 list.Add("public");
11 list.Add("protected");
12 list.Add("private");
13
14 list.Sort();
15
16 search = list.BinarySearch("protected internal");
17 if (search < 0)
18 {
19 /**//*The bitwise complement (~) of this value is
20 * the index of the next element larger than the
21 * element being sought, or the total element count
22 * if there is no greater value. This provides a
23 * convenient means to insert new values into the
24 * list at the specific location so as to maintain sorting */
25 list.Insert(~search, "protected internal");
26 }
27
28 foreach (string accessModifier in list)
29 {
30 Console.WriteLine(accessModifier);
31 }
32 Console.ReadKey();
33 }
34}
Demonstrating FindAll() and Its Predicate Parameter
代码
1using System;
2using System.Collections.Generic;
3using Microsoft.VisualStudio.TestTools.UnitTesting;
4
5class Program
6{
7
8 static void Main()
9 {
10 List<int> list = new List<int>();
11 list.Add(1);
12 list.Add(2);
13 list.Add(3);
14 list.Add(2);
15
16 List<int> results = list.FindAll(Even);
17 /**//*
18 * 命名空间:Microsoft.VisualStudio.TestTools.UnitTesting
19 * 程序集:Microsoft.VisualStudio.QualityTools.UnitTestFramework(在 microsoft.visualstudio.qualitytools.unittestframework.dll 中)
20 */
21 Assert.AreEqual(2, results.Count);
22 Assert.IsTrue(results.Contains(2));
23 Assert.IsFalse(results.Contains(3));
24 }
25 public static bool Even(int value)
26 {
27 if ((value % 2) == 0)
28 {
29 return true;
30 }
31 else
32 {
33 return false;
34 }
35 }
36}
1using System;
2using System.Collections.Generic;
3using Microsoft.VisualStudio.TestTools.UnitTesting;
4
5class Program
6{
7
8 static void Main()
9 {
10 List<int> list = new List<int>();
11 list.Add(1);
12 list.Add(2);
13 list.Add(3);
14 list.Add(2);
15
16 List<int> results = list.FindAll(Even);
17 /**//*
18 * 命名空间:Microsoft.VisualStudio.TestTools.UnitTesting
19 * 程序集:Microsoft.VisualStudio.QualityTools.UnitTestFramework(在 microsoft.visualstudio.qualitytools.unittestframework.dll 中)
20 */
21 Assert.AreEqual(2, results.Count);
22 Assert.IsTrue(results.Contains(2));
23 Assert.IsFalse(results.Contains(3));
24 }
25 public static bool Even(int value)
26 {
27 if ((value % 2) == 0)
28 {
29 return true;
30 }
31 else
32 {
33 return false;
34 }
35 }
36}