C#語言基礎——集合(ArrayList集合)

 

集合及特殊集合數組

集合的基本信息:安全

System.Collections 命名空間包含接口和類,這些接口和類定義各類對象(如列表、隊列、位數組、哈希表和字典)的集合。
System.Collections.Generic 命名空間包含定義泛型集合的接口和類,泛型集合容許用戶建立強類型集合,它能提供比非泛型強類型集合更好的類型安全性和性能。
ystem.Collections.Specialized 命名空間包含專用的和強類型的集合,例如,連接的列表詞典、位向量以及只包含字符串的集合。ide

經常使用的集合爲ArrayList類;特殊集合通常會用到Queue隊列集合、Stack棧集合還有Hashtable哈希表集合。性能

集合不一樣於數組,是一組可變數量的元素的組合,這些元素可能共享某些特徵,須要以某種操做方式一塊兒進行操做。通常來講,這些元素的類型是相同的。spa

集合與數組的區別:數組是連續的、同一類型數據的一塊區域,而集合能夠是不連續的、多種數據類型的。code

1、ArrayList集合:對象

    ArrayList實現了IList、ICollection、IEnumerable接口;blog

  ArrayList與Array的名字很類似,如今來比較一下二者的異同。排序

相同點:索引

  (1)二者都實現了IList、ICollection、IEnumerable接口;

  (2)二者均可以使用整數索引訪問集合中的元素,包括讀取和賦值,且集合中的索引都從0開始。

不一樣點:

  (1)ArrayList是集合,而Array是數組;

  (2)ArrayList是具體類,Array是抽象類;

  (3)數組必須在實例化時指定元素的數量,該數量一旦肯定就不能夠更改了,而ArrayList擴展了這一點,當實例化一個ArrayList實例時能夠不指定集合元素數(有默認初始容量),固然你也能夠指定初始容量;

  (4)獲取數組的元素數時使用Length屬性,而獲取ArrayList集合的元素數時使用Count屬性;

  (5)數組能夠有多維,而ArrayList只能是一維。

ArrayList具體提供的功能:

            ArrayList al = new ArrayList();//初始化

            al.Add(3);

            al.Add(5);

            al.Add(1);

            al.Add(8);

            al.Add(4);

錯誤      由於5號索引以前就沒有,因此直接賦值不能夠

            al[5] = 9;

            al[4] = 9;

            查看個數

            Console.WriteLine(al.Count);

清空集合

al.Clear();

克隆集合

            ArrayList al1 = new ArrayList();

            al1 = (ArrayList)al.Clone();

            Console.WriteLine(al1[2]);

判斷是否包含某個元素

            Console.WriteLine(al.Contains(6));

查看第一次出現該元素的索引號   若沒有,返回-1

            Console.WriteLine(al.IndexOf(1));

查看最後一次出現該元素的索引號

            Console.WriteLine(al.LastIndexOf(9));

在1號索引插入一個4

            al.Insert(1,4);

            Console.WriteLine(al[1]);

移除某個元素的第一次出現的值

            al.Remove(4);

            Console.WriteLine(al[1]);

移除某個索引號上的元素

al.RemoveAt(3);

            Console.WriteLine(al[3]);

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

            {

                Console.WriteLine(al[i]);

            }

            Console.WriteLine();
View Code

       

排序,升序

al.Sort();

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

            {

                Console.WriteLine(al[i]);

            }
View Code

          

改成降序

            al.Reverse();//翻轉集合

例題:一、輸入人數,輸入每個人的分數,求平均分,求最高分,求最低分,寫成集合。

 ArrayList a = new ArrayList();

            double m = 0;

            Console.Write("請輸入人數:");

            int b = int.Parse(Console.ReadLine());

            for (int i = 0; i < b; i++)

            {

                Console.Write("輸入分數:");

                a.Add(double.Parse(Console.ReadLine()));

                m += double.Parse(a[i].ToString());

            }

            Console.Write("平均分爲:"+(m/b));

            a.Sort();

            Console.Write("最低分爲:"+a[0]);

            Console.Write("最高分爲:" + a[b - 1]);

            Console.ReadLine();
View Code

          

二、將每一個人的姓名,年齡存入集合,按照年齡從大到小排列,姓名也須要排列,須要知道年齡最大的是誰。

法一:

 Console.Write("請輸入人數:");

            int n = int.Parse(Console.ReadLine());

            ArrayList xm = new ArrayList();

            ArrayList age = new ArrayList();

            for (int i = 0; i < n; i++)

            {

                Console.Write("請輸入第{0}我的的姓名:", (i + 1));

                xm.Add(Console.ReadLine());

                Console.Write("請輸入{0}我的的年齡:", (i + 1));

                age.Add(int.Parse(Console.ReadLine()));

            }

            for (int i = 0; i < n; i++)

            {

                for (int j = i; j < n - 1; j++)

                {

                    if (int.Parse(age[i].ToString()) < int.Parse(age[j + 1].ToString()))

                    {

                        int zh = int.Parse(age[i].ToString());

                        age[i] = age[j + 1];

                        age[j + 1] = zh;

                        string z = xm[i].ToString();

                        xm[i] = xm[j + 1];

                        xm[j + 1] = z;

                    }

                }

            }

            Console.WriteLine("年齡最大的是{0},是{1}歲", xm[0], age[0]);

            Console.ReadLine();
View Code

          

法二:

Console.WriteLine("請輸入人數:");

            int a = int.Parse(Console.ReadLine());

            ArrayList jh = new ArrayList();

            for (int i = 0; i < a; i++)

            {

                Console.Write("請輸入第{0}人的姓名:", i + 1);

                jh.Add(Console.ReadLine());

                Console.Write("請輸入第{0}人的年齡:", i + 1);

                jh.Add(Console.ReadLine());

            }

            Console.WriteLine();

            for (int i = 1; i < a * 2; i = i + 2)

            {

                for (int j = i; j < a * 2 - 2; j = j + 2)

                {

                    if (int.Parse(jh[i].ToString()) < int.Parse(jh[j + 2].ToString()))

                    {

                        int huan = int.Parse(jh[i].ToString());

                        jh[i] = jh[j + 2];

                        jh[j + 2] = huan;

                        string o = jh[i - 1].ToString();

                        jh[i - 1] = jh[j + 1];

                        jh[j + 1] = o;

                    }

                }

            }

            Console.WriteLine("年齡最大的爲:" + jh[0] + "年齡爲:" + jh[1]);

            Console.ReadLine();
View Code

           

注:借用上一題的定義集合名

●遍歷集合:foreach (object aa in jh)

            {

                Console.WriteLine(aa);

            }

●遍歷數組:int [] array = new int[]{2,3,4,6,7,8,9,2};

            foreach (int aa in array)

            {

                Console.WriteLine(aa+2);

            }

相關文章
相關標籤/搜索