• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎(chǔ)} {C++ 高級} {C#界面,C++核心算法} {設(shè)計模式} {C#基礎(chǔ)}

            c#2.0 List<> 使用

            It is a fairly common programming scenario to find ourselves with a list of identical objects. In the past, without adequate support from programming languages, we found ourselves writing a lot of searching and sorting code, and that may have put you off using lists in favour of arrays. All that has changed with C# (particularly 2.0) - its implementation of a list makes handling such lists remarkably easy.

            For example, given the following class Person:

            public class Person

            {

            ????????? public int age;

            ????????? public string name;

            ????????? public Person(int age, string name)

            ????????? {

            ?????????????????? this.age = age;

            ?????????????????? this.name = name;

            ????????? }

            }

            We can create a list of Person objects and add six people like so:

            List<person>people =

            new List<person>();

            people.Add(

            new Person(50, "Fred"));
            people.Add(
            new Person(30, "John"));
            people.Add(
            new Person(26, "Andrew"));
            people.Add(
            new Person(24, "Xavier"));
            people.Add(
            new Person(5, "Mark"));
            people.Add(
            new Person(6, "Cameron"));

            C#'s list mechanism provides us with a number of useful methods. Personally, I find ForEach, FindAll and Sort to be very useful. ForEach allows us access to each item in the list. FindAll allows us to search for objects in the list that match a specific condition. Sort allows us to sort the objects in the list. The following code demonstrates how we might use each of these methods:

            Console.WriteLine("Unsorted list");

            people.ForEach(

            delegate (Person p)
            ?? { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });

            // Find the young

            List<person> young = people.FindAll( delegate (Person p) { return p.age < 25; });
            Console.WriteLine("Age
            is less than 25");

            young.ForEach(
            delegate (Person p)
            ?? { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });

            // Sort by name

            Console.WriteLine("Sorted list, by name");
            people.Sort(
            delegate (Person p1, Person p2)
            ?? {
            return p1.name.CompareTo(p2.name); });

            people.ForEach(

            delegate (Person p)
            ?? { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });

            // Sort by age

            Console.WriteLine("Sorted list, by age");

            people.Sort(

            delegate (Person p1, Person p2)
            ?? {
            return p1.age.CompareTo(p2.age); });

            people.ForEach(

            delegate (Person p)
            ?? { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });

            And here is the output that we should expect:

            Unsorted list
            50 Fred
            30 John
            26 Andrew
            24 Xavier
            5 Mark
            6 Cameron

            Age is less than 25
            24 Xavier
            5 Mark
            6 Cameron

            Sorted list, by name
            26 Andrew
            6 Cameron
            50 Fred
            30 John
            5 Mark
            24 Xavier

            Sorted list, by age
            5 Mark
            6 Cameron
            24 Xavier
            26 Andrew
            30 John
            50 Fred

            Lists are powerful and result in fewer, and more elegant, lines of code. Hopefully this short example has demonstrated their ease and you will find yourself using them in your day-to-day development activities.

            posted on 2006-04-14 10:20 夢在天涯 閱讀(904) 評論(0)  編輯 收藏 引用 所屬分類: C#/.NET

            公告

            EMail:itech001#126.com

            導航

            統(tǒng)計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1807508
            • 排名 - 5

            最新評論

            閱讀排行榜

            一本久久a久久精品综合香蕉| 国产69精品久久久久观看软件 | 99久久国产亚洲综合精品| 久久人人爽人人澡人人高潮AV| 亚洲午夜无码AV毛片久久| 伊人色综合久久天天人手人婷| 久久99国产综合精品女同| 久久婷婷五月综合色99啪ak| 久久人人爽人人爽人人片av麻烦 | AAA级久久久精品无码区| 中文字幕无码久久精品青草| 国产精品久久自在自线观看| 亚洲美日韩Av中文字幕无码久久久妻妇| 97精品国产97久久久久久免费| 国产美女久久精品香蕉69| 四虎影视久久久免费观看| 99久久99这里只有免费费精品| 伊人情人综合成人久久网小说| 久久精品国产免费| 久久精品国产亚洲AV无码麻豆| 久久久久亚洲AV无码专区桃色| 久久国产免费观看精品| 久久婷婷激情综合色综合俺也去 | 99久久综合国产精品免费| 97久久超碰成人精品网站| 亚洲国产高清精品线久久 | 深夜久久AAAAA级毛片免费看| 久久综合亚洲欧美成人| 久久99九九国产免费看小说| 99热都是精品久久久久久| 国内精品久久久久影院免费| 亚洲日韩中文无码久久| 亚洲精品tv久久久久| 久久精品国产WWW456C0M| 国产2021久久精品| 99久久精品国产一区二区蜜芽| www久久久天天com| 国产成人久久精品激情| 久久精品国产亚洲AV高清热| 亚洲国产精品无码久久一线| 亚洲国产精品无码久久久秋霞2|