zoukankan      html  css  js  c++  java
  • C# System.Collections

    System.collection类:

    动态数组ArrayList

    代表了可被单独索引的对象的有序集合。也就是说他是一个动态的数组,你可以通过索引来进行增删改等操作,数组会自动调整数组的大小。允许在列表进行动态内存的分配、增加、收索和排序。

    Capacity:获取或设置 ArrayList 可以包含的元素个数。
    Count:获取 ArrayList 中实际包含的元素个数。
    IsFixedSize:获取一个值,表示 ArrayList 是否具有固定大小。
    IsReadOnly:获取一个值,表示 ArrayList 是否只读。
    Item:获取或设置指定索引处的元素。

    • public virtual int Add(object value); 在 ArrayList 的末尾添加一个对象。
    • public virtual void AddRange(ICollection c); 在 ArrayList 的末尾添加 ICollection 的元素。
    • public virtual void Clear();从 ArrayList 中移除所有的元素。
    • public virtual bool Contains(object item); 判断某个元素是否在 ArrayList 中。
    • public virtual ArrayList GetRange(int index, int count); 返回一个ArrayList,表示源ArrayList中元素的子集。
    • public virtual int IndexOf(object);返回某个值在ArrayList中第一次出现的索引,索引从零开始。
    • public virtual void Insert(int index, object value); 在ArrayList的指定索引处,插入一个元素。
    • public virtual void InsertRange(int index, ICollection c); 在 ArrayList 的指定索引处,插入某个集合的元素。
    • public virtual void Remove(object obj); 从ArrayList中移除第一次出现的指定对象。
    • public virtual void RemoveAt(int index); 移除ArrayList的指定索引处的元素。
    • public virtual void RemoveRange(int index, int count); 从 ArrayList 中移除某个范围的元素。
    • public virtual void Reverse();逆转 ArrayList 中元素的顺序。
    • public virtual void SetRange(int index, ICollection c); 复制某个集合的元素到 ArrayList 中某个范围的元素上。
    • public virtual void Sort();对 ArrayList 中的元素进行排序。
    • public virtual void TrimToSize();设置容量为 ArrayList 中元素的实际个数。

    哈希表hashtable

    可以使用健来进行访问、排序元素。

    Count:获取Hashtable中包含的键值对个数。

    IsFixedSize:获取一个值,表示Hashtable是否具有固定大小。

    IsReadOnly:获取一个值,表示Hashtable是否只读。

    Item:获取或设置与指定的键相关的值。

    Keys:获取一个 ICollection,包含Hashtable中的键。

    Values:获取一个ICollection,包含Hashtable中的值。

    • public virtual void Add(object key, object value); 向 Hashtable 添加一个带有指定的键和值的元素。
    • public virtual void Clear(); 从 Hashtable 中移除所有的元素。
    • public virtual bool ContainsKey(object key); 判断 Hashtable 是否包含指定的键。
    • public virtual bool ContainsValue(object value); 判断 Hashtable 是否包含指定的值。
    • public virtual void Remove(object key); 从 Hashtable 中移除带有指定的键的元素。
    排序列表sortlist

    可以使用键或值来访问列表中的项。

    Capacity:获取或设置SortedList的容量。

    Count:获取 SortedList 中的元素个数。

    IsFixedSize:获取一个值,表示 SortedList 是否具有固定大小。

    IsReadOnly:获取一个值,表示 SortedList 是否只读。

    Item:获取或设置与 SortedList 中指定的键相关的值。

    Keys:获取 SortedList 中的键。

    Values:获取 SortedList 中的值。

    • public virtual void Add(object key, object value); 向 SortedList 添加一个带有指定的键和值的元素。
    • public virtual void Clear(); 从 SortedList 中移除所有的元素。
    • public virtual bool ContainsKey(object key); 判断 SortedList 是否包含指定的键。
    • public virtual bool ContainsValue(object value); 判断 SortedList 是否包含指定的值。
    • public virtual object GetByIndex(int index); 获取 SortedList 的指定索引处的值。
    • public virtual object GetKey(int index); 获取 SortedList 的指定索引处的键。
    • public virtual IList GetKeyList(); 获取 SortedList 中的键。
    • public virtual IList GetValueList(); 获取 SortedList 中的值。
    • public virtual int IndexOfKey(object key); 返回 SortedList 中的指定键的索引,索引从零开始。
    • public virtual int IndexOfValue(object value); 返回 SortedList 中的指定值第一次出现的索引,索引从零开始。
    • public virtual void Remove(object key); 从 SortedList 中移除带有指定的键的元素。
    • public virtual void RemoveAt(int index); 移除 SortedList 的指定索引处的元素。
    • public virtual void TrimToSize(); 设置容量为 SortedList 中元素的实际个数。
    堆栈stack

    后进先出队列。Count:获取堆栈中的元素个数。

    • public virtual void Clear(); 从 Stack 中移除所有的元素。
    • public virtual bool Contains( object obj ); 判断某个元素是否在 Stack 中。
    • public virtual object Peek();返回在 Stack 的顶部的对象,但不移除它。
    • public virtual object Pop();移除并返回在 Stack 的顶部的对象。
    • public virtual void Push( object obj );向 Stack 的顶部添加一个对象。
    • public virtual object[] ToArray();复制 Stack 到一个新的数组中。
    队列queue

    先进先出队列。Count:获取队列中的元素个数。

    • public virtual void Clear(); 从 Queue 中移除所有的元素。
    • public virtual bool Contains(object obj); 判断某个元素是否在 Queue 中。
    • public virtual object Dequeue();移除并返回在 Queue 的开头的对象。
    • public virtual void Enqueue( object obj ); 向 Queue 的末尾添加一个对象。
    • public virtual object[] ToArray();复制 Queue 到一个新的数组中。
    • public virtual void TrimToSize();设置容量为 Queue 中元素的实际个数。
     点阵列BitArray

     代表了使用1和0来表示的二进制数组。

    Count:获取 BitArray 中包含的元素个数。

    IsReadOnly:获取一个值,表示 BitArray 是否只读。

    Item:获取或设置 BitArray 中指定位置的位的值。

    Length:获取或设置 BitArray 中的元素个数。

    • public BitArray And(BitArray value); 对当前的BitArray中的元素和指定的BitArray中的相对应的元素执行按位与操作。
    • public bool Get(int index); 获取BitArray中指定位置的位的值。
    • public BitArray Not();把当前的BitArray中的位值反转,以便设置为true的元素变为false,设置为false的元素变为 true。
    • public BitArray Or(BitArray value); 对当前的BitArray中的元素和指定的BitArray中的相对应的元素执行按位或操作。
    • public void Set(int index, bool value); 把BitArray中指定位置的位设置为指定的值。
    • public void SetAll(bool value); 把BitArray中的所有位设置为指定的值。
    • public BitArray Xor(BitArray value); 对当前的BitArray中的元素和指定的BitArray中的相对应的元素执行按位异或操作。

    简单实用ArrayList:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Collections;
    
    namespace ConsoleApplication3
    {
        public class Student
        {
            public int nameNo { get; set; }
            public string Name { get; set; }
            public override string ToString()
            {
                return string.Format("{0}的学号是{1}", this.Name, this.nameNo);
            }
        }
        public class Clazz:IEnumerable
        {
            public ArrayList students = new ArrayList();
            public int AddNewStudent(Student stu)
            {
                return this.students.Add(stu);
            }
            // 实现GetEnumerator接口
            public IEnumerator GetEnumerator()
            {
                foreach(object item in this.students)
                {
                    yield return item;
                }
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                /*
                 * ============================== ArayList ==============================
                // 使用ArrayList创建集合
                ArrayList ab = new ArrayList();  // 创建一个集合
                // 可以添加任何类型的东西
                ab.Add("a");
                ab.Add(123);
                ab.Add(true);
                // object类型
                foreach (object item in ab)
                {
                    Console.Write(item);
                    Console.Write("	");
                }
                Console.WriteLine();
                // 可以编译通过,但是执行会出错
                *
                foreach(int item in ab)
                {
                    // 错误原因在于不是整型时,需要进行类型转换,解决办法通过类型转换即可,循环时先使用"object item",然后再"(int)item"进行类型转换
                    // 不过如果进行了类型转换就涉及到了一个新的问题,那就是装箱和拆箱的效率问题
                    Console.Write(item);
                    Console.Write("	");
                }
                Console.WriteLine();*
                // 计算个数
                Console.WriteLine(ab.Count);
                // 当然也可以通过下标进行访问
                for (int i = 0; i < ab.Count; i++)
                {
                    Console.Write(ab[i]);
                    Console.Write("	");
                }
                Console.ReadKey();
                */
                //////////////////////////////////////////////////
                /*
                // arraylist的另一种用法,对类进行操作
                ArrayList al = new ArrayList();
                al.Add(new Student() { nameNo = 1, Name = "小王" });
                al.Add(new Student() { nameNo = 2, Name = "小李" });
                al.Add(new Student() { nameNo = 5, Name = "小龙" });
                foreach(object item in al)
                {
                    Console.WriteLine(item);
                }
                Console.Clear();  // 清屏
                System.Collections.Hashtable NoLIst = new Hashtable();  // 创建指定的序号进行排序
                var s1 = new Student() { nameNo = 1, Name = "小也子" };
                var s2 = new Student() { nameNo = 2, Name = "小青子" };
                var s3 = new Student() { nameNo = 3, Name = "小岚子" };
                var s4 = new Student() { nameNo = 4, Name = "小宝子" };
                // 也可以获取前面的值
                var s5 = al[2] as Student;
                // 根据nameNo索引进行排序
                NoLIst.Add(s1.nameNo, s1);
                NoLIst.Add(s2.nameNo, s2);
                NoLIst.Add(s3.nameNo, s3);
                NoLIst.Add(s4.nameNo, s4);
                NoLIst.Add(s5.nameNo, s5);
                foreach(object item in NoLIst)
                {
                    Console.WriteLine(item);  // 输出"System.Collections.DictionaryEntry"
                }
                Console.Clear();
                // 因为上面输出我们知道是"System.Collections.DictionaryEntry"类型
                foreach(DictionaryEntry item in NoLIst)
                {
                    Console.WriteLine("key:{0},value:{1}", item.Key, item.Value);  // 输出
                }
                Console.Clear();
                Student s = NoLIst[2] as Student;  // 弱类型
                Console.WriteLine("no:{0}, value:{1}", s.nameNo, s.Name);*/
                //
                // ==================================== IEnumerable =================================
                /*
                ArrayList al = new ArrayList();
                al.Add(new Student() { nameNo = 1, Name = "小王" });
                al.Add(new Student() { nameNo = 2, Name = "小李" });
                al.Add(new Student() { nameNo = 5, Name = "小龙" });
                IEnumerable enumer = al;
                IEnumerator iet = enumer.GetEnumerator();  // 返回的是True或者false
                // 往下一个元素移动
                while (iet.MoveNext())
                {
                    object currentItem = iet.Current;  // 获取当前元素
                    Console.WriteLine(currentItem);  // 输出当前元素
                }*/
                var s1 = new Student() { nameNo = 1, Name = "小也子" };
                var s2 = new Student() { nameNo = 2, Name = "小青子" };
                var s3 = new Student() { nameNo = 3, Name = "小岚子" };
                var s4 = new Student() { nameNo = 4, Name = "小宝子" };
                Clazz clazz = new Clazz();
                clazz.AddNewStudent(s1);
                clazz.AddNewStudent(s2);
                clazz.AddNewStudent(s3);
                clazz.AddNewStudent(s4);
                // 1.Calzz没有继承IEnumerable
                /*
                 * 报错,因为并没有包含IEnumerable
                foreach(Student item in clazz)
                {
                    //
                }*/
                // 2.Calzz继承IEnumerable,并实现GetEnumerator
                IEnumerator iet3 = clazz.GetEnumerator();
                while (iet3.MoveNext())
                {
                    Console.WriteLine(iet3.Current);
                }
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    using System.Collections;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                /*
                //============================================= SortList ===============================
                SortedList sl = new SortedList();  // 初始化
                // 添加健值
                sl.Add("1", "bob1");
                sl.Add("2", "bob2");
                sl.Add("3", "bob3");
                sl.Add("4", "bob4");
                sl.Add("5", "bob5");
                // 此外还有 sl.Contains(健)、sl.ContainsKey(健)
                if (sl.ContainsValue("bob4"))
                {
                    // 判断是否包含某些值
                    Console.WriteLine("已包含");
                }
                else
                {
                    sl.Add("4", "bob4");
                }
                ICollection keys = sl.Keys;  // 获取健的集合
                ICollection values = sl.Values;  // 获取所有值
                foreach (string mykey in keys)
                {
                    Console.Write(mykey+" ");
                }
                Console.WriteLine();
                foreach (string myvalue in keys)
                {
                    Console.Write(myvalue+" ");
                }*/
                //
                /*
                //================================== stack ================================
                Stack sk = new Stack();  // 初始化
                // 进堆
                sk.Push("a1");
                sk.Push("a2");
                sk.Push("a3");
                sk.Push("a4");
                sk.Push("a5");
                Console.WriteLine(sk.Count);  // 获取堆的个数
                *
                // 遍历
                foreach (string item in sk)
                {
                    Console.Write(item + " ");
                }*
                //
                // Console.WriteLine(sk.Peek());  // 获取顶部元素,但是不删除
                //
                // Console.WriteLine(sk.Pop());  // 弹出(并删除)一个顶部元素
                */
                //
                /*
                //================================ queue ==========================
                Queue que = new Queue();  // 初始化
                que.Enqueue("bob1");
                que.Enqueue("bob2");
                que.Enqueue("bob3");
                que.Enqueue("bob4");
                que.Enqueue("bob5");
                //
                // Console.WriteLine(que.Count);
                //
                // que.Clear();  // 清空队列
                *
                //遍历队列
                foreach (string mystr in que)
                {
                    Console.Write(mystr+" ");
                }
                *
                //Console.WriteLine(que.Dequeue());  // 弹出(并删除)头部队列
                */
                //
                //================================ BitArray ===================================
                // 创建两个大小为8的点阵列
                BitArray ba1 = new BitArray(8);
                BitArray ba2 = new BitArray(8);
                byte[] a = { 60 };
                byte[] b = { 13 };
                // 把60和13,添加到点阵列中
                ba1 = new BitArray(a);
                ba2 = new BitArray(b);
                //Console.WriteLine(ba1.Count);  // 获取元素数量
                // 获取ba1的内容
                for (int i = 0; i < ba1.Count; i++)
                {
                    Console.Write("{0,-6}", ba1[i]);
                }
                Console.WriteLine();
                // 获取ba2的内容
                for (int i = 0; i < ba2.Count; i++)
                {
                    Console.Write("{0,-6}", ba2[i]);
                }
                Console.WriteLine();
                BitArray ba3 = new BitArray(8);
                /*
                ba3 = ba1.And(ba2);  // 执行and运算
                // 获取ba3的内容
                for (int i = 0; i < ba3.Count; i++)
                {
                    Console.Write("{0,-6}", ba3[i]);
                }
                Console.WriteLine();*/
                ba3 = ba1.Or(ba2);  // 进行or运算
                for (int i = 0; i < ba3.Count; i++)
                {
                    Console.Write("{0,-6}", ba3[i]);
                }
            }
        }
    }
  • 相关阅读:
    爬虫——Selenium与PhantomJS
    爬虫——多线程糗事百科案例
    爬虫——json模块与jsonpath模块
    爬虫——使用BeautifulSoup4的爬虫
    爬虫——BeautifulSoup4解析器
    爬虫——爬取百度贴吧每个帖子里面的图片
    爬虫——爬虫中使用正则表达式
    爬虫——正则表达式re模块
    爬虫——requests模块
    爬虫——Handler处理器 和 自定义Opener
  • 原文地址:https://www.cnblogs.com/namejr/p/10433151.html
Copyright © 2011-2022 走看看