1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Unity-ArrayList List HashTable Dictionary

Unity-ArrayList List HashTable Dictionary

时间:2021-09-18 07:45:58

相关推荐

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);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。