集合相关
列表是动态数组,是有序的即输入和存储顺序一致
字典是无序的(元素个数多了就能体现出来)
列表
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using UnityEngine; using System.Collections;//非泛型集合命名空间 using System.Collections.Generic;//泛型集合命名空间 /// <summary> /// 列表 /// </summary> public class ArraylistTest : MonoBehaviour { private ArrayList list = new ArrayList(); private List<string> list02 = new List<string>(); private List<int> list03 = new List<int>() { 1,10,5,100}; public GameObject cube; private void Start() { list02.Add("赵六"); list03.Sort(); foreach (int item in list03) { print("排序后: " + item); } list.Add(1); list.Add("张三"); list.Add("李四"); list.Add(cube); for (int i = 0; i < list.Count; i++) { print("01: " + list[i]); } } private void Update() { /* * * * */ if (Input.GetKeyDown(KeyCode.Space)) { //list[0] = list[3]; /*判断集合中含有张三的元素,将其移除 if (list.Contains("张三")) //list.Remove("张三"); list.RemoveAt(1); * */ //在索引1位置插入王五元素 //list.Insert(1, "王五"); //从索引1位置移除2个元素 list.RemoveRange(1, 2); for (int i = 0; i < list.Count; i++) { print("02: " + list[i]); } } } }
字典
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using UnityEngine; using System.Collections.Generic; /// <summary> /// 字典(泛型) /// </summary> public class DictionaryTest : MonoBehaviour { private Dictionary<string, string> test = new Dictionary<string, string>(); private Dictionary<string, int> weapon = new Dictionary<string, int>(); private Dictionary<string, Dictionary<string, string>> name = new Dictionary<string, Dictionary<string, string>>(); private void Start() { name.Add("王五", new Dictionary<string, string>()); name["王五"].Add("电话", "11111"); name["王五"].Add("QQ", "22222"); print(name["王五"]["电话"] + " " + name["王五"]["QQ"]); test.Add("张三", "11111"); weapon.Add("流星锤", 10000); weapon.Add("斧子", 10); print(weapon["流星锤"]); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using UnityEngine; using System.Collections; /// <summary> /// 字典(非泛型) /// </summary> public class HashtableTest : MonoBehaviour { private Hashtable table = new Hashtable(); private void Start() { table.Add("张", "123"); table.Add("李", "345"); table.Add("周", "567"); table.Add("赵", "789"); print("张电话: "+table["张"]); foreach (string num in table.Values) { print(num); } foreach (string num in table.Keys) { print("Key : " + num + " Value : " + table[num]); } } }
队列
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using UnityEngine; using System.Collections; /// <summary> /// 队列(先进先出) 买票 /// /// </summary> public class QueueTest : MonoBehaviour { private Queue queue = new Queue(); private void Start() { queue.Enqueue("入队"); queue.Dequeue();//出队 queue.Peek();//读队首 print(queue.Count); } }
堆栈
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using UnityEngine; using System.Collections; /// <summary> /// 栈(后进先出) /// 压栈Push() /// 弹栈Pop() 读取并移除顶部对象 /// 读栈顶Peek() 读取顶部对象但不移除 /// count /// /// 主菜单->选项->游戏选项->难度调节->困难 /// 逐层压栈,逐层弹栈,最后一个只读不弹栈(主菜单) /// </summary> public class StackTest : MonoBehaviour { private Stack stack = new Stack(); private void Start() { stack.Push("主菜单"); stack.Push("选项"); stack.Push("游戏选项"); stack.Push("难度调节"); stack.Push("困难"); while(stack.Count>1) { print("弹栈 : " + stack.Pop()); } print("只读不弹栈 : " + stack.Peek()); } }
谢谢