Unity-ArrayList,List,HashTable,Dictionary

//1 List比数组的优势在哪。
//数组的长度不能扩容,是固定的。
//2 List比Arraylist的优势在哪。
//Arraylist执行效率低(有装箱和拆箱过程),不如List执行效率高。(List是Arraylist的泛型版本)。
//3 Arraylist比list和数组的优势在哪。
//ArrayList集合里可以加不同类型的元素。例如第一个元素是int类型,第二个元素是string类型。

//没有集合里的数据元素是不同类型这个需求的话,推荐用List类型。

//4 Dictionary比HashTable的优势在哪。
//HashTable执行效率低(有装箱和拆箱过程),不如Dictionary执行效率高。(Dictionary是HashTable的泛型版本)。
//5 HashTable比Dictionary的优势在哪。
//HashTable里可以加不同类型的元素。例如第一个元素添加key是int类型,value是int类型,第二个元素key是string类型,value是string类型。

//没有集合里的数据元素是不同类型这个需求的话,推荐用Dictionary类型。

//6 定义一个数组,第一个元素是int类型1,第二个元素类型是int类型2。
//7 定义一个ArrayList,第一个元素是int类型1,第二个元素是string类型"2"。
//8 定义一个List,第一个元素是int类型3,第二个元素是int类型4。
//9 list在位置2插入值为10的值。
//10 list里删除值为6的第一个数据,后面有值为6的数据的话,不删除。
//11 查找list里有无数据10,有的话返回它的位置,没有返回-1,有的话返回当前位置。
//12 把list转换为数组。
//13 定义一个HashTable,添加一个key为int类型,value为int类型的值。添加一个key为string类型,value为string类型。
//14 定义一个Dictionary,添加一个key为int类型,value为int类型的值。添加一个key为int类型,value为int类型。
//15 Dictionary检测是否含有值为1的Key。
//16 Dictionary检测是否含有值为1的value。
//17 根据Dictionary的一个key,删除这个数据。
//18 定义一个Dictionary,添加一个value为自定义类对象的数据。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Demo : MonoBehaviour
{

    public ArrayList arrayList = new ArrayList();
    public List<int> list = new List<int>();

    public Hashtable ht = new Hashtable();
    public Dictionary<int, int> dic = new Dictionary<int, int>();

    Test test = new Test();

    public Hashtable ht2 = new Hashtable();
    public Dictionary<int, Test> dic2 = new Dictionary<int, Test>();

    int[] intArray;

    void Start()
    {
        //数组是固定长度,使用前,需要先定义固定的长度。
        intArray = new int[2];
        intArray[0] = 0;//不定义new int[2];这里直接取intArray[0]会报错。
        intArray[1] = 1;//不定义new int[2];这里直接取intArray[1]会报错。

        foreach (var item in intArray)
        {
            Debug.Log("intArray item = " + item);
        }

        //ArrayList一个优势,可以加不同类型的元素
        arrayList.Add(1);
        arrayList.Add("2");

        foreach (var item in arrayList)
        {
            Debug.Log("arrayList item = " + item);
        }

        list.Add(3);
        list.Add(4);


        foreach (var item in list)
        {
            Debug.Log("list item = " + item);
        }

        //位置2插入数值为10的数据,插入的位置必须在list长度内,如果超出长度会报错。
        list.Insert(2, 10);

        foreach (var item in list)
        {
            Debug.Log("list.Insert(2, 10) item = " + item);
        }

        //list里删除值为6的第一个数据,后面有值为6的数据的话,不删除。如果没有值为6的数据不会报错。
        list.Remove(6);

        foreach (var item in list)
        {
            Debug.Log("list.Remove(6) item = " + item);
        }

        //查找list里有无数据10,有的话返回它的位置,没有返回-1,有的话返回当前位置。
        Debug.Log("list.IndexOf(10) = " + list.IndexOf(10));


        //list转为数组
        int[] array = list.ToArray();
        foreach (var item in array)
        {
            Debug.Log("array item = " + item);
        }

        //Hashtable有一个优势,可以增加不同类型的数据
        ht.Add(1, 101);
        ht.Add("2", "102");
        dic.Add(1, 101);
        dic.Add(2, 102);

        //根据键删除
        ht.Remove(1);
        dic.Remove(1);

        //如果没有键值为5的数据不会报错。
        ht.Remove(5);
        dic.Remove(5);

        //判断是否包含键
        ht.ContainsKey(1);
        dic.ContainsKey(1);

        //判断是否包含值
        ht.ContainsValue(1);
        dic.ContainsValue(1);

        Debug.Log("dic[2] = " + dic[2]);
        try
        {
            //强行读取字典里没有键的数据会报错
            Debug.Log("dic[5] = " + dic[5]);
        }
        catch (System.Exception ex)
        {
            Debug.Log("ex = " + ex);
        }

        //哈希表和字典的值可以为复杂的class类型
        ht2.Add(1,test);
        dic2.Add(1,test);
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值