List and ArrayList

本文介绍了使用C#进行列表操作的方法,包括如何利用BinarySearch查找元素并保持列表排序,以及如何使用FindAll方法结合Predicate参数筛选特定元素。
List<> and ArrayList Class Diagrams

Using the Bit Complement of the BinarySearch() Result
ContractedBlock.gifExpandedBlockStart.gif代码
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifclass Program
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5InBlock.gif    static void Main()
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 7InBlock.gif        List<string> list = new List<string>();
 8InBlock.gif        int search;
 9InBlock.gif
10InBlock.gif        list.Add("public");
11InBlock.gif        list.Add("protected");
12InBlock.gif        list.Add("private");
13InBlock.gif
14InBlock.gif        list.Sort();
15InBlock.gif
16InBlock.gif        search = list.BinarySearch("protected internal");
17InBlock.gif        if (search < 0)
18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
19ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//*The bitwise complement (~) of this value is 
20InBlock.gif             * the index of the next element larger than the 
21InBlock.gif             * element being sought, or the total element count 
22InBlock.gif             * if there is no greater value. This provides a 
23InBlock.gif             * convenient means to insert new values into the 
24ExpandedSubBlockEnd.gif             * list at the specific location so as to maintain sorting */

25InBlock.gif            list.Insert(~search, "protected internal");
26ExpandedSubBlockEnd.gif        }

27InBlock.gif
28InBlock.gif        foreach (string accessModifier in list)
29ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
30InBlock.gif            Console.WriteLine(accessModifier);
31ExpandedSubBlockEnd.gif        }

32InBlock.gif        Console.ReadKey();
33ExpandedSubBlockEnd.gif    }

34ExpandedBlockEnd.gif}
 

Demonstrating FindAll() and Its Predicate Parameter
ContractedBlock.gifExpandedBlockStart.gif代码
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing Microsoft.VisualStudio.TestTools.UnitTesting;
 4None.gif
 5None.gifclass Program
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif
 8InBlock.gif    static void Main()
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
10InBlock.gif        List<int> list = new List<int>();
11InBlock.gif        list.Add(1);
12InBlock.gif        list.Add(2);
13InBlock.gif        list.Add(3);
14InBlock.gif        list.Add(2);
15InBlock.gif
16InBlock.gif        List<int> results = list.FindAll(Even);
17ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
18InBlock.gif         * 命名空间:Microsoft.VisualStudio.TestTools.UnitTesting
19InBlock.gif         * 程序集:Microsoft.VisualStudio.QualityTools.UnitTestFramework(在 microsoft.visualstudio.qualitytools.unittestframework.dll 中)
20ExpandedSubBlockEnd.gif         */

21InBlock.gif        Assert.AreEqual(2, results.Count);
22InBlock.gif        Assert.IsTrue(results.Contains(2));
23InBlock.gif        Assert.IsFalse(results.Contains(3));
24ExpandedSubBlockEnd.gif    }

25InBlock.gif    public static bool Even(int value)
26ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
27InBlock.gif        if ((value % 2== 0)
28ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
29InBlock.gif            return true;
30ExpandedSubBlockEnd.gif        }

31InBlock.gif        else
32ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
33InBlock.gif            return false;
34ExpandedSubBlockEnd.gif        }

35ExpandedSubBlockEnd.gif    }

36ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/nanshouyong326/archive/2007/05/10/741889.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值