List集合操作一:遍歷與查找


首先聲明本文不是討論Linq,在Framwork2.0中也不支持linq操作的,主要是記錄一下List集合的使用方法。

List<T> 一般主要用到的查找遍歷方法:
Find:搜索指定謂詞所定義條件相匹配的元素,並返回整個List<T>中的第一個匹配元素。
FindLast:搜索指定謂詞所定義條件相匹配的元素,並返回整個List<T>中的最后一個匹配元素。
Find:搜索指定謂詞所定義條件相匹配的元素,並返回整個List<T>中的第一個匹配元素。
FindLast:搜索指定謂詞所定義條件相匹配的元素,並返回整個List<T>中的最后一個匹配元素。
FindAll:檢索與指定謂詞定義條件匹配的所有元素。
FindIndex:搜索指定謂詞所定義條件相匹配的元素,並返回整個List<T>中的第一個匹配元素從0開始的索引,未找到返回-1。
FindLastIndex:搜索指定謂詞所定義條件相匹配的元素,並返回整個List<T>中的最后一個匹配元素從0開始的索引,未找到返回-1。
ForEach:對每個List<T>執行指定的操作。參數 Action委托。

注意:
List<T>Find(), List<T>FindLast()的不同是, List<T>Find()由Index=0開始尋找, 而List<T>FindLast()由Index = List<T>.Count - 1開始尋找.

我們創建一個控制台程序並參照一下代碼實例:

首先創建一個實體類Player:

using System.Collections.Generic;

namespace ListDemo1
{
    public class Player
    {
        public Player(int id, string name, string team)
        {
            this.Id = id;
            this.Name = name;
            this.Team = team;
        }

        public int Id { get; set; }

        public string Name { get; set; }

        public string Team { get; set; }
    }
}

創建一個集合類PlayerList繼承List<Player>:

using System.Collections.Generic;

namespace ListDemo1
{
    public class PlayerList : List<Player>
    {
        public PlayerList()
        {
            this.Add(new Player(1, "科比-布萊恩特", "湖人隊"));
            this.Add(new Player(2, "保羅-加索爾", "湖人隊"));
            this.Add(new Player(3, "拉瑪爾-奧多姆", "湖人隊"));
            this.Add(new Player(4, "德克-諾維茨基", "小牛隊"));
            this.Add(new Player(5, "傑森-特里", "小牛隊"));
            this.Add(new Player(6, "肖恩-馬里昂", "小牛隊"));
            this.Add(new Player(7, "凱文-加內特", "凱爾特人隊"));
        }
    }
}

Main 函數代碼:

using System;
using System.Collections.Generic;

namespace ListDemo1
{
    class Program
    {
        static void Main(string[] args)
        {
            PlayerList players = new PlayerList();
            //查找所有隊員數據:
            Action<Player> listAll = delegate(Player p)
            {
                Console.WriteLine(string.Format("隊員Id={0} | 隊員名稱={1} | 所屬球隊={2} ", p.Id, p.Name, p.Team));
            };
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(string.Format("數據庫所有隊員信息為:"));
            players.ForEach(listAll);

            //查找小牛隊隊員的謂詞定義
            Predicate<Player> findValue = delegate(Player p)
            {
                return p.Team.Equals("小牛隊");
            };
            //第一個匹配元素
            Player firstPlayer = players.Find(findValue);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("\r\nFind方法獲得第一個小牛隊員是:{0}", firstPlayer.Name);

            //獲得所有的匹配元素
            List<Player> findPlayers = players.FindAll(findValue);
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(string.Format("\r\nFindAll方法查找到的所有小牛球員為:"));
            Action<Player> listFindAll = delegate(Player p)
            {
                Console.WriteLine(string.Format("隊員Id={0} | 隊員名稱為={1}", p.Id, p.Name));
            };
            findPlayers.ForEach(listFindAll);
            Console.ReadKey();
        }
    }
}

運行結果如下圖:

至此本篇List集合的遍歷與查找介紹完畢,下一篇主要介紹一下List集合的排序方式

-=源碼下載=-


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM