zoukankan      html  css  js  c++  java
  • unity之Hashtable ArrayList List

    1.用于处理key-value这样的键值对,key区分大小写,value为object类型。可通过key来快速查找内容。

    private class Pdata{
    public string pname = "the data";

    }
    void Start () {

    Hashtable h=new Hashtable();
    Pdata pd=new Pdata();
    pd.pname="new name";
    h.Add("1",pd);

    pd=new Pdata();
    h.Add("2",pd);
    pd=(Pdata)h["2"];
    Debug.Log(pd.pname);

    foreach (DictionaryEntry dt in h) {//哈希表的遍历
    Debug.Log(dt.Key); //键,,dt.Value则为你加入的值
    }

    }

    2.    ArrayList 是动态数组,功能同Array一样,或者说更复杂一点。简单的使用方法如下:

    ArrayList arr = new ArrayList ();
    for (int i=0; i<25; i++) {
    arr.Add("a"+i);//添加一个元素
    }
    string str="a10";
    arr.Remove(str);//通过元素引用删除
    arr.Remove("a0");
    arr.RemoveAt (0);//移除0位置上的元素
    arr.Insert(0,"a11");//添加一个元素到指定位置,列表后面的元素依次往后移动
    Queue myQueue = new Queue();
    myQueue.Enqueue( "a12" );
    myQueue.Enqueue( "a13" );

    arr.AddRange (myQueue);//添加一个队列至列表末尾

    int len = arr.Count;//数组长度

    Debug.Log(arr.Capacity);//数组容量
    arr.TrimToSize ();//        如果不向集合中添加新元素,则此方法可用于最小化集合的内存系统开销。若要将 ArrayList 重置为它的初始状态,请在调用 TrimToSize 之前用 Clear 法。


    Debug.Log(arr.Capacity);

    关于数组扩容

    ArrayList的默认容量是16,每次向数组新增元素时都会检查容量够不够,不够则在当前数组容量上扩容一倍,

    将旧元素Copy到新数组中,然后丢弃旧数组。若是能预估数组长度,则可在开始设置长度 ,如下:

    ArrayList arr = new ArrayList (300);

    而不必经历16*2*2*2*2*2=512五次扩容,还留有212个空白位置。

    3.List

    功能与ArrayList相似,不同的是List需要确定类型,而ArrayList什么类型都可以装,进行操作的时候会有类型检测,这也导致了ArrayList要比List慢。实例化方法如下:

    List<GameObject> restCoin=new List<GameObject>();

    4.还有一种数组表示方法,例如float[] ft=new float[5],二维 float[][] dts=new float[13][];在确定类型和数组位数的情况下可使用这种。但是没有任何增删方法,最是省内存。

  • 相关阅读:
    「2021.08 实战」Springboot wagon 2.x 部署线上(自动重启)
    Springboot 使用 CacheManager(缓存是储存到内存中去的,程序结束,缓存也没了)
    [Linux]在CentOS中安装Python3的方法
    [VS2015]关闭_CRT_SECURE_NO_WARNINGS警告
    STL源码剖析-智能指针shared_ptr源码
    透明网桥-消除循环功能
    MPEG-PS封装格式
    RTP timestamp与帧率及时钟频率的关系
    Pyhton @staticmethod
    Python @classmethod
  • 原文地址:https://www.cnblogs.com/sevenmoons/p/4095135.html
Copyright © 2011-2022 走看看