C#编程--泛型集合

一、集合:

集合的长度是不固定的。

1. 集合命名空间:

   using System.Collections; (非泛型集合)
   using System.Collections.Generic;(泛型集合)

2. 常用集合

  • 类似数组” 集合: ArrayList、List<T>;
  • 键值对” 集合(“哈希表”集合): Hashtable、Dictionary<K,V>
  • 堆栈” 集合: Stack、Stack<T> ;       Last In First Out
  • 队列” 集合: Queue、Queue<T> ;       First In First Out
  2.1 ArrayList集合:



增加元素、删除元素

namespace 集合
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList arrayList = new ArrayList();
            Console.WriteLine("集合中元素的个数为{0}", arrayList.Count);
            Console.WriteLine("集合现在的容量为{0}", arrayList.Capacity);
            //向集合中添加元素
            arrayList.Add(21);
            Console.WriteLine("集合中元素的个数为{0}", arrayList.Count);
            Console.WriteLine("集合现在的容量为{0}", arrayList.Capacity);
            arrayList.Add("21");
            arrayList.Add("xxx");
            Person p1 = new Person();
            p1.Name = "....";
            arrayList.Add(p1);
            
            Console.WriteLine("集合中元素的个数为{0}", arrayList.Count);
            Console.WriteLine("集合现在的容量为{0}", arrayList.Capacity);

            arrayList.AddRange(new int[]{1,3,5,7,8,10,12});
            Console.WriteLine("集合中元素的个数为{0}", arrayList.Count);
            Console.WriteLine("集合现在的容量为{0}", arrayList.Capacity);

            //向指定的位置插入一个元素
            arrayList.Insert(0, "www");
            //向指定的位置插入一个元素
            arrayList.InsertRange(5, new string[] { "a", "b" });

            //删除元素
            //想要将元素全部删除,直接clear();
            //     RemoveAt:按照索引去删除元素
            //     Remove:  根据对象来删除
            arrayList.RemoveAt(5);
            arrayList.Remove(p1);

            //在数组中,想要知道数组长度用.Length,而所有的集合使用的是.Count。
            //通过下标获取元素
            for (int i = 0; i < arrayList.Count; i++)
            {
                Console.WriteLine(arrayList[i]);
            }
            Console.ReadKey();
        }       
    }
    public class Person
    {
        public string Name
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }
        public string Email
        {
            get;
            set;
        }

    }
}

拼接两个arrayList集合,并将相同的元素去掉。

        static void Main(string[] args)
        {
            ArrayList arr1 = new ArrayList();
            ArrayList arr2 = new ArrayList();
            string[] s1 = new string[] { "a", "b", "c", "d", "e" };
            arr1.AddRange(s1);
            string[] s2 = new string[] { "d", "e", "f", "g", "h" };
            arr2.AddRange(s2);
            for (int i = 0; i < arr2.Count;i++ )
            {
                if(!arr1.Contains(arr2[i]))
                {
                    arr1.Add(arr2[i]);
                }
            }

                for (int i = 0; i < arr1.Count; i++)
                {
                    Console.WriteLine(arr1[i]);
                }
                Console.ReadKey();
        }
  2.2 HashTable集合:

   HashTable 键值对集合,类似于字典,HashTable在查找元素的时候,速度非常快。

namespace 对象1
{
    class Program
    {
        static void Main(string[] args)
        {
            Hashtable hash = new Hashtable();
            //键值对集合的键不能重复。(唯一) 
            //判断集合中是否已经存在某个键
            if(!hash.ContainsKey("h1"))
            {
                hash.Add("h1", "xxx");
            }
            hash.Add("xzl", new Person() { Name="sss"});
            //通过键获取值
            Console.WriteLine(hash["h1"].ToString());
            Person p1 = hash["xzl"] as Person;
            Console.WriteLine(p1.Name);
            //遍历hashtable
            //    1. 遍历键: 使用foreach遍历     //////////////////////////////////////
            foreach(var i in hash.Keys)
            {
                Console.WriteLine("键{0}——值{1}", i, hash[i]);
            }
            //    2. 直接遍历值                 //////////////////////////////////////
            foreach (var i in hash.Values)
            {
                Console.WriteLine("值{0}", i);
            }
            //    3. 直接遍历键值对             //////////////////////////////////////
            foreach(DictionaryEntry kv in hash)
            {
                Console.WriteLine("键{0}    值:{1}", kv.Key,kv.Value);
            }
                Console.ReadKey();
        }
    }
    class Person
    {
        public Person()
        {
        }
        public Person(string name, int age, int height)
        {
            this.Name = name;
            this.Age = age;
            this.Height = height;
        }
        public Person(string name)
            : this(name, 0, 0)
        {
        }

        public string Name
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }
        public int Height
        {
            get;
            set;
        }
    }
}

3.泛型集合

泛型集合的用处更多更大更方便,因此大多情况下使用的都是泛型集合。

        static void Main(string[] args)
        {
            //泛型集合,存储的元素都是相同的类型的。
            List<string> list = new List<string>();
            List<int> list1 = new List<int>();
            list1.Add(1000);
            list1.Add(2000);
            list1.Add(3000);
            

            //HashTable的泛型版本,Dictionary
            //字典类型是HashTable的升级版本,在定义的时候必须指明键,和值的数据类型。
            Dictionary<string, int> dict = new Dictionary<string, int>();
            dict.Add("WANG", 100);
            dict.Add("xxx", 99);

            Console.WriteLine(dict["xxx"]);
            //遍历键
            foreach(string item in dict.Keys)
            {
                Console.WriteLine(item);
            }
            //遍历值
            foreach (int item in dict.Values)
            {
                Console.WriteLine(item);
            }
            //直接遍历键值对
            foreach (KeyValuePair<string,int> item in dict)
            {
                Console.WriteLine("键{0}   值{1}",item.Key,item.Value);
            }
            Console.ReadKey();
        }
            Mat text = new Mat(10,10,DepthType.Cv32F,1);
            unsafe
            {
                float* data = (float*)text.DataPointer;
                for (int i = 0; i < text.Rows; i++)
                {
                    for (int j = 0; j < text.Cols; j++)
                    {
                        data[i * text.Step + j] = 0.86f;
                    }
                }
            }
            List<float> a1 = new List<float>();
            unsafe
            {
                float* data = (float*)text.DataPointer;
                for (int i = 0; i < text.Rows; i++)
                {
                    for (int j = 0; j < text.Cols; j++)
                    {
                        a1.Add(data[i * text.Step + j]);
                    }
                }
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值