unity游戲開發(fā)-C#語言基礎(chǔ)篇(泛型結(jié)合與類及數(shù)組應(yīng)用)

 class Program
    {
        public static Dictionary<string, int> dic = new Dictionary<string, int>();
        static void Main(string[] args)
        {
            //泛型集合=字典數(shù)組/集合 重點(diǎn)標(biāo)識(shí)符 <>  沒有的話普通集合 和泛型集合


            dic.Add("張三", 28);
            dic.Add("李四", 28);
            dic.Add("王五", 18);
            dic.Add("趙六", 18);//鍵不能一樣,值可以一樣;

            dic.Comparer(); 比較返回布爾值
            if (dic.ContainsKey("張三")==false)
            {
                dic.Add("張三", 28);//判斷是否存在,不存在就添加
            }

             TianJia("張三",38);//同類中直接調(diào)用 或者類名.方法

            dic.Remove("張三");

            foreach (string s in dic.Keys)//返回鍵 如果value 就是值;
            {
                // Console.WriteLine(s);
            }

            //數(shù)組的鍵值字典

            Dictionary<int, int[]> dicArr = new Dictionary<int, int[]>();

            dicArr.Add(3, new int[] { 11, 22, 33, 44, 55 });
            dicArr.Add(2, new int[] { 11, 22, 33, 88, 55 });
            dicArr.Add(1, new int[] { 11, 22, 33, 44, 55 });
            dicArr.Add(0, new int[] { 11, 22, 33, 44, 55 });

            int n = dicArr[1][3];//像交錯(cuò)數(shù)組 有索引器 鍵[0]值[0] 取出數(shù)組里的元素
            //交錯(cuò)數(shù)組里面的行列都要一致;
            //二維數(shù)組里面的列元素個(gè)數(shù)可以不同;

            Dictionary<string, HeroClass> dicHero = new Dictionary<string, HeroClass>();

            dicHero.Add("谷主", new HeroClass("鬼王", 42));
            dicHero.Add("手下", new HeroClass());

            //dicHero["谷主"].ShowMsg();//有索引器 傳的是鍵 不是下標(biāo) 
            // dicHero["手下"].ShowMsg();//第一種方法顯示

            dicHero["谷主"].Name = "鬼王宗";
            foreach (HeroClass hero in dicHero.Values) //前面類型必須跟要取值的類型必須一樣;第二種方法顯示
            {
                //hero.ShowMsg();

            }

            //鍵值對(duì)字典組合:數(shù)組和類混合
            Dictionary<string, HeroClass[]> dicArrHero = new Dictionary<string, HeroClass[]>();
            HeroClass[] zhengpaiArr = new HeroClass[] { new HeroClass("青云門", 1000), new HeroClass("天音閣", 500), new HeroClass("888", 500) };
            HeroClass[] mojiaoArr = new HeroClass[] { new HeroClass("鬼王宗", 1000), new HeroClass("毒門", 500) };
            dicArrHero.Add("正派", zhengpaiArr);
            dicArrHero.Add("魔派", mojiaoArr);

            //dicArrHero["魔派"][1].ShowMsg();

            //遍歷
            foreach (string i in dicArrHero.Keys)
            {
                if (i == "魔派")
                {
                    for (int j = 0; j < dicArrHero["魔派"].Length; j++)
                    {
                        dicArrHero["魔派"][j].ShowMsg();
                    }
                }
                else if (i == "正派")
                {
                    for (int j = 0; j < dicArrHero["正派"].Length; j++)
                    {
                        dicArrHero["正派"][j].ShowMsg();
                    }
                }
                for (int j = 0; j < dicArrHero[i].Length; j++)
                {
                    dicArrHero[i][j].ShowMsg();
                }
            }






            //--------------------list------------

            List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            //list.Add(23);
            //Console.WriteLine(list.Capacity  );//容量
            // Console.WriteLine(list.Count);//元素個(gè)數(shù)
            Console.WriteLine(list[2]);//索引器

            list.AddRange(new List<int>() { 11, 22, 33 });
            list.Insert(3, 888);//從指定下標(biāo)插入元素
            list.InsertRange(1, new List<int>() { 55, 66, 77 });//指定下標(biāo)插入數(shù)組,不覆蓋原來的元素
            list.Remove(888);//移除指定元素
            list.RemoveAt(1);//移除指定下標(biāo)元素; 
            list.RemoveRange(1, 3);//移除指定下標(biāo)的元素個(gè)數(shù) 參數(shù)1:下標(biāo) 參數(shù)2:元素個(gè)數(shù);
            list.Sort();//從小排到大
            list.Reverse();//反轉(zhuǎn)數(shù)組

            //List<T> T表示所有類型


            foreach (int a in list)
            {
                Console.WriteLine("元素:{0}", a);
            }




            List<HeroClass[]> listHero = new List<HeroClass[]>();









            Console.ReadKey();
        }
        public static void TianJia(string key, int value)
        {


            if (dic.ContainsKey(key) == false)
            {
                dic.Add(key, value);
            }
            else
            {

                Console.WriteLine("已經(jīng)存在!");
            }

        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容