日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

C#中的集合学习笔记

發(fā)布時間:2023/12/20 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#中的集合学习笔记 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

  前言:漫長的國慶長假終于快結(jié)束了,我們該工作的工作了,該學習的學習了,該干什么的也都開始了,這篇博客我主要說一下C#中集合的使用,也就是ArrayList集合和Hashtable字典集合的使用,我在這里說的主要還是簡單的使用,使學習的能夠很快的學習集合然后自己研究集合的一些高級用法,在最后還列舉出了一些常用的小案例。

  • ArrayList
  • (1) 命名空間 System.Collection

    ???? (2)創(chuàng)建對象

    ??????????? 1)增

    ?????????????????? ->Add

    ?????????????????? ->AddRange? 任何一個集合對象

    ????????????????????????? int[] num = { 1, 2, 3, 4, 5, 6 };

    ????????????????????????? ArrayList arr = new ArrayList();

    ????????????????????????? //ICollection一個對象里面包含很多其他對象

    ????????????????????????? arr.AddRange(num);

    ?????????????????? ->Insert

    ??????????? 2)刪

    ?????????????????? ->Remove

    ?????????????????? ->RemoveAt

    ?????????????????? ->Clear

    ??????????? 3)改

    ?????????????????? ->像數(shù)組一樣通過索引來修改

    ??????????? 4)查

    ?????????????????? ->Contains

    ?????????????????? ->IndexOf

  • 自定義集合
  • (1) 集合是Object類型,處理數(shù)據(jù)的時候會需要強轉(zhuǎn)等操作

    ???? (2)舉例說明:

    1)新建一個Person類class Person{public string Name { get; set; }public int Age { get; set; }public char Gender { get; set; }public Person(string n, int a, char g){Name = n;Age = a;Gender = g;}public void SayHello(){Console.WriteLine("我叫{0}", Name);}}2)新建一個PeresonCollection類class PersonCollection{ArrayList arr = new ArrayList();public void Add(Person p){arr.Add(p);}public void AddRange(params Person[] ps){arr.AddRange(ps);}public Person this[int index]{get { return (Person)arr[index]; }}public int Count{get { return arr.Count; }}}3)在main方法中實現(xiàn)static void Main(string[] args){PersonCollection ps = new PersonCollection();ps.Add(new Person("韓迎龍", 23, '男'));ps.Add(new Person("韓全龍", 16, '男'));ps.Add(new Person("泛亞內(nèi)", 16, '男'));ps.Add(new Person("野田", 16, '男'));for (int i = 0; i < ps.Count; i++){ps[i].SayHello();}Console.ReadKey();}

    ???? (3)定義索引

    ??????????? 1)就是一個通過傳進來的參數(shù)訪問內(nèi)部數(shù)據(jù)的一個屬性(帶有參數(shù)的屬性)

    ??????????? 2)public 返回類型 this[索引參數(shù)類型 參數(shù)名]

    ??????????? ? {

    ?????????????????? get{return 通過參數(shù)訪問的數(shù)據(jù);}

    ?????????????????? set{通過參數(shù)處理要修改的數(shù)據(jù)=value}

    ??????????? ? }

    ??????????? ? 案例說明:? ?

    class Arr{int[] nums = { 10, 23, 34, 45, 56 };public int this[int index]{get {if (index > nums.Length - 1){throw new Exception("索引超出異常");}return nums[index];}set { nums[index] = value; }}public int Count{get { return nums.Length; }}}class IndexString{int num = 10;string str = "字符串韓迎龍";char ch = 'c';public int this[int index]{get { return num; }}public string this[string str]{get { return this.str; }}public char this[char c]{get { return ch; }}public string this[int n, char c, string s]{get { return num + str + ch; }}}class Program{static void Main(string[] args){IndexString IS=new IndexString();Console.WriteLine(IS[' ']);Console.WriteLine(IS[0, ' ', " "]);Console.ReadKey();}}

    ??????????? 3)模擬實現(xiàn)集合

    class MyCllection{ArrayList arrInt = new ArrayList();ArrayList arrStr = new ArrayList();ArrayList obj=new ArrayList();public void Add(string key, object obj){int index = arrStr.Add(key);arrInt.Add(index);this.obj.Add(obj);}public object this[int index]{get { return obj[index]; }}public object this[string key]{get { return obj[arrStr.IndexOf(key)]; }}}static void Main(string[] args){MyCllection mc = new MyCllection();mc.Add("HYL", "韓迎龍");mc.Add("HYL", "韓全龍");Console.WriteLine(mc[0]);Console.WriteLine(mc["HYL"]);Console.ReadKey();}
  • foreach語法
  •     (1) 執(zhí)行

    ??????????? 1)可以只遍歷一部分嗎?? 不能

    ??????????? 2)可以倒著遍歷嗎?????? 不能

    ??????????? 3)舉例說明:

    ??????????? ??? int[] nums = { 1, 4, 6, 87, 87, 3, 23, 45 };

    ??????????? foreach (int item in nums)

    ??????????? {

    ??????????????? Console.WriteLine(item);

    ??????????? }

    ??????????? Console.ReadKey();

    ???? (2)數(shù)據(jù)源應該是一個可枚舉的類型(IEnumerable)

    ???? (3)foreach循環(huán)的步驟

    ??????????? 1)foreach循環(huán)首先到數(shù)據(jù)集中調(diào)用GetEnumerator方法得到枚舉器對象

    ??????????? 2)到in關鍵字,調(diào)用MoveNext()方法

    ??????????? 3)這個方法返回bool值,將枚舉器移到數(shù)據(jù)集的下一個數(shù)據(jù)上,并檢查是否有數(shù)據(jù),如果有,返回true,否則返回false

    ??????????? 4)到臨時變量(迭代變量),開始調(diào)用枚舉器的Current屬性,將當前的數(shù)據(jù)取出來,然后賦值給臨時變量

    ??????????? 5)就可以使用了

  • 嵌套類
  • (1) 有時候在某個類型中需要一個特定的類型處理數(shù)據(jù),而這個類型只在當前對象類中可使用

    ?(2)可以將這個類定義在一個類的里面,此時稱為嵌套類

    (3)嵌套類的訪問修飾符和一般成員一樣

    ?(4)一般是為了考慮安全問題

  • yield迭代器
  • (1) 一般的語法:yield return 數(shù)據(jù)

    ?(2)foreach在執(zhí)行開始訪問數(shù)據(jù)集的時候,依舊是調(diào)用IEnumerable中的GetEnumerator方法

    (3)這個Getenumerator方法可以使用這個迭代器返回數(shù)據(jù)

    (4)在方法中寫上循環(huán),循環(huán)體上放上yield,那么循環(huán)不會立即結(jié)束

    (5)每當執(zhí)行到y(tǒng)ield的時候就會停在那里,跳到foreach中

    (6)foreach執(zhí)行到in的時候,又會跳到上次停留的yield那里,繼續(xù)向下執(zhí)行

    (7)直到下一個yield,再次調(diào)用foreach

    ??????????? public IEnumerator GetEnumerator()

    ??????????? {

    ?????????????????? for(int i=0;i<arr.Count;i++)

    ?????????????????? {

    ????????????????????????? yield return arr[i];

    ?????????????????? }

    ??????????? }

  • Hashtable 字典集合
  • (1) 舉例說明:

    ? ? ? ? ? ?Hashtable has = new Hashtable();

    ??????????? has.Add("BigBoss", "韓迎龍");

    ??????????? has.Add("sdd", "的事實");

    ??????????? has.Add("dfd", "的速度");

    ??????????? Console.WriteLine(has["BigBoss"]);

    ???? (2)Hashtable的使用,實現(xiàn)一個電子詞典? ? ?

    class Program{static void Main(string[] args){string[] words = File.ReadAllLines("英漢詞典TXT格式.txt", Encoding.Default);Hashtable has = new Hashtable();for (int i = 0; i < words.Length; i++){string[] temp = words[i].Split(new char[] { ' '}, StringSplitOptions.RemoveEmptyEntries);if (temp.Length == 1){continue;}string key = temp[0];string value = string.Join(" ", temp, 1, temp.Length - 1);if (!has.ContainsKey(key)){has.Add(key, value);}else{has[key] = string.Format("{0}\n{1}", has[i], value);}}//可以查看了while (true){Console.Write("請輸入單詞?");string str=Console.ReadLine();if (has.ContainsKey(str)){Console.WriteLine(has[str]);}else{Console.WriteLine("很抱歉,你說查詢的詞匯沒有找到");}}Console.ReadKey();}}static void Main(string[] args){string[] words = File.ReadAllLines("英漢詞典TXT格式.txt", Encoding.Default);Hashtable has = new Hashtable();for (int i = 0; i < words.Length; i++){string[] temp = words[i].Split(new char[] { ' '}, StringSplitOptions.RemoveEmptyEntries);string key = temp[0];string value = temp[1];if (temp.Length != 2){continue;}if (!has.ContainsKey(key)){has.Add(key, value);}else{has[key] = string.Format("{0}\n{1}", has[i], value);}}//可以查看了while (true){Console.Write("請輸入單詞?");string str=Console.ReadLine();if (has.ContainsKey(str)){Console.WriteLine(has[str]);}else{Console.WriteLine("很抱歉,你說查詢的詞匯沒有找到");}}Console.ReadKey();}}

    ???? (3)如何遍歷?

    static void Main(string[] args){Hashtable has = new Hashtable();has.Add("1", "韓迎龍");has.Add("2", "得到");//僅僅遍歷keyforeach (string item in has.Keys){Console.WriteLine(item);}//遍歷valueforeach (string item in has.Values){Console.WriteLine(item);}//遍歷整個集合foreach (DictionaryEntry item in has){Console.WriteLine("{0}是{1}", item.Key, item.Value); ;}Console.ReadKey();}

    ???? (4)var 類型推斷

    ??????????? 1)var val=數(shù)據(jù);

    ??????????? 2)賦什么數(shù)據(jù)就是什么類型

    ??????????? 3)類型確定以后就不能變了

    ??????????? 4)必須保證類型賦值時時確定的

    ??????????? 5)何時使用類型推斷

    ?????????????????? ->匿名類型

    static void Main(string[] args){var person = new { Name = "張三", Age = 23, Gender = '男' };Console.WriteLine(person.Name);Console.WriteLine(person.Age);Console.WriteLine(person.Gender);Console.ReadKey();}

    ?????????????????? ->Linq查詢

    static void Main(string[] args){int[] nums = { 132, 454, 667, 343, 65, 76, 74 };var query = from n in numswhere n > 100 && n < 500select n;foreach (var item in query){Console.WriteLine(item);}Console.ReadKey();}

    總結(jié)

    以上是生活随笔為你收集整理的C#中的集合学习笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。