设为首页 加入收藏

TOP

C#集合。(一)
2019-09-17 18:33:43 】 浏览:30
Tags:集合

集合命名空间:

  using system.collections. 非泛型集合

  using system.collections.Generic.  泛型集合

为什么要用集合:

  1、数组一旦声明长度就固定了。

  2、集合有很多方法可以用

  等

常用集合:

类似数组集合:ArrayList  List<>

键值对集合:Hashtable  Dictionary<K V>

栈集合:Stack

队列:Queye

ArrayList:

 class Program
    {      
        static void Main(string[] args)
        {
            ArrayList arrayList = new ArrayList();
            arrayList.Add(1); //增加一个元素
            arrayList.AddRange(new int[] { 2, 3 });//增加一个集合
            arrayList.Insert(0, "开始:");//在指定位子插入一个元素
            arrayList.InsertRange(4, new string[] { "", "", "" });//指定位置插入集合
            arrayList.RemoveAt(7);
            arrayList.Clear();//清空集合
            for (int i = 0; i < arrayList.Count; i++)
            {
                Console.WriteLine(arrayList[i]);
            }
        }    
    }

Remove()不是根据对象来判断的,而是根据值来判断。

Contains()是否包含某个值,也是根据值来判断的。

集合转为数组:.ToArray().

排序:  1、.sort() 升序  没有降序   可以用.Reverse()颠倒位置实现。

    2、想让任何集合实现排序需要实现 IComparable接口 。

    3、直接调用sort是使用ICompareble 接口的默认方式来排序。可以用sort的重载,使用自己的比较器。

Hashtable:

简单添加和获取:

class Program
    {      
        static void Main(string[] args)
        {
            Hashtable hashtable = new Hashtable();
            hashtable.Add("Sam", "Sam");
            hashtable.Add("Penny", new Person() { Name = "Penny" });
            Console.WriteLine(hashtable["Sam"]);
            Person Penny = hashtable["Penny"] as Person;
            Console.WriteLine(Penny.Name);

        }    
    }

键值对集合的键不能重复。

判断是否存在某个键:.ContentsKey()    是否存在某个值:.ContentsValue()

遍历Hash table:

class Program
    {      
        static void Main(string[] args)
        {
            Hashtable hashtable = new Hashtable();
            hashtable.Add("Sam", "Sam");
            hashtable.Add("Penny", "Penny");
            //遍历键
            foreach (object item in hashtable.Keys)
            {
                Console.WriteLine("Key:{0}----Value:{1}",item,hashtable[item]);
            }
            //遍历值
            foreach (object item in hashtable.Values)
            {
                Console.WriteLine("Value:{0}",item);
            }
            //遍历键值对
            foreach (DictionaryEntry item in hashtable)
            {
                Console.WriteLine("Key:{0}---Value{1}",item.Key,item.Value);
            }

        }    
    }

集合小练习:

两个ArrryList集合的并集。

class Program
    {      
        static void Main(string[] args)
        {
            ArrayList A = new ArrayList(new string[] {"a","b"});
            ArrayList B = new ArrayList(new string[] { "a", "b","c","d" });
            for (int i = 0; i <B.Count; i++)
            {
                if (!A.Contains(B[i]))
                {
                    A.Add(B[i]);
                }
            }
            for (int i = 0; i < A.Count; i++)
            {
                Console.WriteLine(A[i]);
            }
        }    
    }

随机生成十个1-100之间的数放到arraylist中,这些数必须是偶数且不能重复。

class Program
    {      
        static void Main(string[] args)
        {
            ArrayList arrayList = new ArrayList();
            Random random = new Random();
            while (arrayList.Count<10)
            {
                int r = random.Next(1, 101);
                if (!arrayList.Contains(r)&&((r%2)==0))
                {
                    arrayList.Add(r);
                }
            }
            for (int i = 0; i < arrayList.Count; i++)
            {
                Console.WriteLine(arrayList[i]);
            }
        }    
    }

上面程序把random放循环外面效率更好,因为无参的random构造函数以系统时间为种子,而一遍循环完了以后时间还没来得及变,就会生成相同的数。

泛型集合: 

List<string> list = new List<string> () 

和 arraylist相比:

  数据类型固定,有更多的方法可以用。

Dictionary<string, int> dic = new Dictionary<string, int> ;

和hashtable相比:

  键和值都有类型约束。

遍历键值对:

 class Program
    {      
        static void Main(string[] args)
        {
            Dictionary<string, in
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇GraphQL ---02 GraphQL和C#结合的.. 下一篇在Bootstrap开发框架中使用Grid++..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目