C#集合

来源:互联网 发布:mac 待机快捷键 编辑:程序博客网 时间:2024/06/06 06:46
1,/动态数组 ArrayList jihe存值取值 (他的内部封装了一个object数组)
容量用Capacity 长度用 count而不用length    用Add添加数据  用下标改变数据
2,原理和stringbuild的一样,
 ArrayList a1 = new ArrayList();
            a1.Add(1);
            a1.Add(2);      
            a1[1] = 10;
            //Console.WriteLine(a1[0]);
            foreach (object i in a1)
            { Console.WriteLine(i); }
            Console.WriteLine(a1.Count);
            Console.WriteLine(a1.Capacity);
3,他的所有元素都是object的  很耗性能 从而有了泛型
 List<int> list = new List<int>();//把类型当参数  优势
            list.Add(12);
            Console.WriteLine ( list[0]);
            Console .WriteLine (  list.Capacity);//函数和ArraList集合差不多一样
4  List<int> list = new List<int>(100);//优势把类型当参数  可以加容量
5,栈,  没有容量的概念   类封装的方法  元素object
           Stack stack = new Stack();
            stack.Push(1);
            stack.Push(2);
            stack.Push(3);
            foreach (object i in stack)
           { 
                Console.WriteLine(i); 
            }//321
            Console.WriteLine(stack.Peek()); 只看不取 
           // Console.WriteLine( stack.Pop());//3悔棋时用的。。。。。。。。。。
            //Console.WriteLine(stack.Pop());//2
            //Console.WriteLine(stack.Pop());//1
6,  if (stack.Contains(1))用来判断“1”是否在栈中
                Console.WriteLine("存在");
7,泛型的栈list<int>  stack=new list<int>();
stack.push(1);用法和普通栈一样
8,队列 先进先出  object元素 没容量//用于下棋回放
 Queue queue = new Queue();
            queue.Enqueue(1);
            queue.Enqueue(2);
            queue.Enqueue(3);
           Console .WriteLine ( queue.Dequeue());//1
           Console.WriteLine(queue.Dequeue());//2
           Console.WriteLine(queue.Dequeue());//3
            //foreach (object i in queue)
            //{
            //    Console.WriteLine(i); 
            //}123
9,队列的泛型
list<int> queue=new list<int>();用法和队列一样
10, Hashtable  一个键对应一个值  键/不可以重复
以键作为索引  。。。
 Hashtable ht = new Hashtable();
            ht.Add(1, "1111");
            ht.Add(2, "2222");
            ht.Add(3, "3333");
            ht.Add(4, "4444");
            Console.WriteLine(ht[1]);//1111
不能用foreach遍历 无规律 因为地址是算出来的
他适用于根据键去找值
11,hastable 不好玩 obj  从而有
dictionary<int ,string> dic=new dictionary<int ,string>();
dic.Add(1,"111");
有count;
待更新。。。。 
 


C集合 - 风未定 - 风未定的博客
0 0