zoukankan      html  css  js  c++  java
  • c#中Dictionary、ArrayList、Hashtable和数组的区别是什么?[转]

    C# 集合类 Array Arraylist List Hashtable Dictionary Stack Queue

    1.      数组是固定大小的。但不能伸缩虽然System.Array.Resize这个泛型方法可以重置数组的大小。但该方法是重新创建新设置大小的数组,用的是旧的数组初始化新的数组。所以之前的数组就报废,而集合是可以变长的。
    2. 数组要声明元素的类型,集合的元素类型确实object.
    3. 数组可读可写不能声明为只读数组,集合可以提供ReadOnly方法以只读方式使用集合。
    4. 数组要有整数的下标才能访问特定的元素,然而很多时候这样的下标不是很有用,集合也是数据列表但不能使用下标访问。

    声明数组:int[] array;

    int[] array=new int[3];

    int[] attay=new int[3]{1,3,2};

    int[] array =new int[]{1,2,3};
    int[] intArray1;

    ArrayList被设计成一个动态的数组类型。其容量会随着需要适当的扩充。

    方法:

    Add();

    Remove();

    RemoveAt(int i);

    Reverse();

    Sort();

    Clone();
    List是ArrayList的泛型,类型比较安全,避免了拆箱装箱。 
    Dictionary

    表示键和值的集合。Dictionary遍历输出的顺序,就是加入的顺序,这点与Hashtable不同
    //SortedList类
    与哈希表类似,区别在于SortedList中的Key数组排好序的
    //Hashtable类
    哈希表,名-值对。类似于字典(比数组更强大)。哈希表是经过优化的,访问下标的对象先散列过。如果以任意类型键值访问其中元素会快于其他集合。
    GetHashCode()方法返回一个int型数据,使用这个键的值生成该int型数据。哈希表获取这个值最后返回一个索引,表示带有给定散列的数据项在字典中存储的位置。
    Stack

    栈,后进先出,不允许遍历,Push 方法入栈,pop方法入栈。 
    Queue类
    队列,先进先出。enqueue方法入队列,dequeue方法出队列。
    -------------------------------------------------------------
    //Dictionary
    System.Collections.DictionaryEntry dic=new System.Collections.DictionaryEntry("key1","value1");
    Dictionary<int, string> fruit = new Dictionary<int, string>();
    //加入重复键会引发异常
    fruit.Add(1, "苹果");
    fruit.Add(2, "桔子");
    fruit.Add(3, "香蕉");
    fruit.Add(4, "菠萝");
    //因为引入了泛型,所以键取出后不需要进行Object到int的转换,值的集合也一样
    foreach (int i in fruit.Keys)
    {
    Console.WriteLine("键是:{0} 值是:{1}",i,fruit);
    }
    //删除指定键,值
    fruit.Remove(1);
    //判断是否包含指定键
    if (fruit.ContainsKey(1))
    {
    Console.WriteLine("包含此键");
    }
    //清除集合中所有对象
    fruit.Clear();
    }
    //ArrayList
    System.Collections.ArrayList list=new System.Collections.ArrayList();
    list.Add(1);
    list.Add(2);
    for(int i=0;i<list.Count;i++)
    {
    System.Console.WriteLine(list[i]);
    }
    //List
    //声明一个List对象,只加入string参数
    List<string> names = new List<string>();
    names.Add("乔峰");
    names.Add("欧阳峰");
    names.Add("马蜂");
    //遍历List
    foreach (string name in names)
    {
    Console.WriteLine(name);
    }
    //向List中插入元素
    names.Insert(2, "张三峰");
    //移除指定元素
    names.Remove("马蜂");
    //HashTable
    System.Collections.Hashtable table=new System.Collections.Hashtable();
    table.Add("table1",1);
    table.Add("table2",2);
    System.Collections.IDictionaryEnumerator d=table.GetEnumerator();
    while(d.MoveNext())
    {
    System.Console.WriteLine(d.Entry.Key);
    }
    //Queue
    System.Collections.Queue queue=new System.Collections.Queue();
    queue.Enqueue(1);
    queue.Enqueue(2);
    System.Console.WriteLine(queue.Peek());
    while(queue.Count>0)
    {
    System.Console.WriteLine(queue.Dequeue());
    }
    //SortedList
    System.Collections.SortedList list=new System.Collections.SortedList();
    list.Add("key2",2);
    list.Add("key1",1);
    for(int i=0;i<list.Count;i++)
    {
    System.Console.WriteLine(list.GetKey(i));
    }
    //Stack
    System.Collections.Stack stack=new System.Collections.Stack();
    stack.Push(1);
    stack.Push(2);
    System.Console.WriteLine(stack.Peek());
    while(stack.Count>0)
    {
    System.Console.WriteLine(stack.Pop());
    }
    本文摘自:http://hi.baidu.com/tpasp/blog/item/f42fc1f60258a32b720eec46.html

  • 相关阅读:
    有用的sql语句积累
    spring boot sso 学习资源
    notepad++ 常用快捷键
    artTemplate的使用总结
    ajax完整请求
    03 Mybatis框架---学习笔记1--框架的概念及优势
    02 Struts2框架----学习笔记2(了解一下,已过时)
    IDEA 获取类的相对路径和绝对路径
    01 Struts2框架学习(了解一下,已过时)
    喜马拉雅 FM 已购付费音频下载
  • 原文地址:https://www.cnblogs.com/lovezhangyu/p/3049710.html
Copyright © 2011-2022 走看看