Collections_Script.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Collections_Script : MonoBehaviour
{
/*集合
* 集合特点:
* 一种数据容器,一种数据结构
* 容纳多个数据,大小可变,空间不一定连续
*
* 集合两大体系:非泛型集合,泛型集合
*
* 命名空间:
* 非泛型集合 System.Collections
* 泛型集合 System.Collections.Generic
*
* 非泛型缺点:
* 1、性能不好,因为可能发生装箱
* 2、类型不安全,可能会发生类型转换的异常
* 3、使用不方便,用的时候需要手动做类型转换。
*
* 泛型跟非泛型的区别是:泛型的数据类型更加规范
*
* 列表---非泛型:ArrayList 泛型:List<T> T(数据类型)
*
* 字典---非泛型: HashTable 泛型:Dictionary<TKey,TValue>
* · 一键对应一值,无序输出
* · 不能重复键
* · 可以用键名进行查找,查找方便
* · 可以通过键删除/获取数值
* · ------如----------
* · 键 值
* ·人名 电话号码
*
* 栈 Stack(后进先出) Stack<T> 泛型和非泛型的都是Stack,泛型的需要指定数据类型
* · 后进先出
* · 例子:浏览器撤销、人走路退回来、123321
* · Push()压栈
* · Pop()弹栈 读取并移除顶部对象,注意,移除后数量会跟着减少
* · Peek()读栈顶 读取但不移除顶部对象
* · count 读取栈的统计数量
*
* 队列 Queue(先进先出) Queue<T> 泛型和非泛型的都是Queue,泛型的需要指定数据类型
* · 先进先出
* · Enqueue () 入队 对应Push()压栈
* · Dequeue () 出对 对应Pop()弹栈 读取并移除队首对象,注意,移除后数量会跟着减少
* · Peek () 读队首 读取但不移除队首对象
* · count 读取队列的统计数量
* · 例子:银行拿号、买票
*
*/
#region 列表讲解段-ArrayList、List<T>
//列表---非泛型:ArrayList 泛型:List<T> T(数据类型)
//非泛型:ArrayList
private ArrayList ArrayListVar;
public GameObject Cube;
//泛型:List 定义数据类型为string 字符串类型
private List<string> ListVar;
private void Start1()
{
#region 非泛型:ArrayList讲解--已注释
/*
//非泛型:ArrayList 什么类型都可以放
ArrayListVar = new ArrayList();
ArrayListVar.Add(Cube);
ArrayListVar.Add(0);
ArrayListVar.Add("阿巴阿巴");
ArrayListVar.Add(0.5f);
//ArrayList统计数据使用的是ArrayList.Count
//ArrayList特点:会根据Add新增的数据有序输出,谁先Add进来的,谁先输出出来--重点
for (int i = 0; i < ArrayListVar.Count; i++)
{
print("输出01 " + ArrayListVar);
}
*/
#endregion
//泛型:List
ListVar = new List<string>();
ListVar.Add("123");
ListVar.Add("456");
ListVar.Add("789");
ListVar.Add("阿巴阿巴");
if (ListVar.Contains("789"))
{
//以下是练习
//根据对应的元素值从数组移除
//ListVar.Remove("789");
//根据对应的数组索引从数组移除对应的元素值
//ListVar.RemoveAt(2);
//根据对应的数组索引范围从数组移除对应的元素值
//注意,范围值从1开始就已经要移除,至后面1个元素,共移除两个元素!!--重点
ListVar.RemoveRange(1, 2);
ListVar.Insert(2, "666");
}
for (int i = 0; i < ListVar.Count; i++)
{
print("ListVar输出01 " + ListVar);
}
print("现在 开始ListVar输出01 排序");
//对整个 ArrayList 中的元素进行排序。
ListVar.Sort();
for (int i = 0; i < ListVar.Count; i++)
{
print("ListVar输出01 " + ListVar);
}
print("开始ListVar输出01 排序完成");
}
private void Update1()
{
#region 非泛型:ArrayList讲解--已注释
/*
if (Input.GetKeyDown(KeyCode.Space))
{
//非泛型:ArrayList 可以互相赋不同数据类型的值,不建议此方法使用,否则容易出现数据类型转换报错异常
ArrayListVar[3] = ArrayListVar[0];
}
for (int i = 0; i < ArrayListVar.Count; i++)
{
print("输出02 " + ArrayListVar);
}
*/
#endregion
if (Input.GetKeyDown(KeyCode.Space))
{
ListVar[3] = "duang";
for (int i = 0; i < ListVar.Count; i++)
{
print("输出02 " + ListVar);
}
}
}
#endregion
#region 字典讲解段-HashTable、Dictionary<TKey,TValue>
/*
* 字典---非泛型: HashTable 泛型:Dictionary<TKey,TValue>
* · 一键对应一值,无序输出
* · 不能重复键
* · 可以用键名进行查找,查找方便
* · 可以通过键删除/获取数值
* · ------如----------
* · 键 值
* ·人名 电话号码
*/
#region 字典 非泛型:HashTable
private Hashtable HashtableVar = new Hashtable();
private void Start2()
{
//注意:字典的键一般建议使用字符串作为键的名称,数值的话处理效率很慢
//增加字典Hashtable的键值
HashtableVar.Add("dza", 1323784512);
HashtableVar.Add("dmh", 11719316);
HashtableVar.Add("lyy", 786145123);
HashtableVar.Add("ftf", 452251248);
//HashtableVar.Count 为统计字典数量的字段,不同于数组的长度
print("这个字典的键值数量有"+HashtableVar.Count);
//字典Hashtable无序输出值(注意:不输出对应的键)
foreach (string HashValues in HashtableVar.Values)
{
print(HashValues);
}
//字典Hashtable无序输出键和对应的值方法
foreach (string Hashkeys in HashtableVar.Keys)
{
print("键是 " + Hashkeys + " 值是 " + HashtableVar[Hashkeys]);
}
}
#endregion
#region 字典 泛型:Dictionary<TKey,TValue>
//字典 泛型:Dictionary<TKey,TValue> 定义键和值的数据类型都为string 字符串类型
private Dictionary<string,string> DictionaryVar = new Dictionary<string,string>();
//字典嵌套字典
private Dictionary<string, Dictionary<string, string>> DictionaryVarFor = new Dictionary<string, Dictionary<string, string>>();
private string KeyName;
private void Start3()
{
//增加字典Dictionary的键值
//DictionaryVar.Add("d", "测试1");
//DictionaryVar.Add("m", "测试2");
//DictionaryVar.Add("l", "测试3");
//DictionaryVar.Add("f", "测试4");
//嵌套字典主键和子健的赋值,注意,主键赋值时,子健需要先new初始化,再使用主键查找赋值子健
//代码段重复度,以后建议自己创建方法使用
KeyName = "张三";
DictionaryVarFor.Add(KeyName, new Dictionary<string, string>());
DictionaryVarFor[KeyName].Add("地址","中国");
DictionaryVarFor[KeyName].Add("手机号", "123456");
//打印嵌套字典的方法
print(KeyName + DictionaryVarFor[KeyName]["地址"] + DictionaryVarFor[KeyName]["手机号"]);
KeyName = "李四";
DictionaryVarFor.Add(KeyName, new Dictionary<string, string>());
DictionaryVarFor[KeyName].Add("地址", "美国");
DictionaryVarFor[KeyName].Add("手机号", "654321");
print(KeyName + DictionaryVarFor[KeyName]["地址"] + DictionaryVarFor[KeyName]["手机号"]);
//字典Dictionary无序输出值(注意:不输出对应的键)
foreach (string DictionaryValues in DictionaryVar.Values)
{
print(DictionaryValues);
}
//字典Dictionary无序输出键和对应的值方法
foreach (string Dictionarykeys in DictionaryVar.Keys)
{
print("键是 " + Dictionarykeys + " 值是 " + DictionaryVar[Dictionarykeys]);
}
}
#endregion
#endregion
#region 栈讲解段-Stack(后进先出) Stack<T>
/*
* 栈 Stack(后进先出) Stack<T> 泛型和非泛型的都是Stack,泛型的需要指定数据类型
* · 后进先出
* · 例子:浏览器撤销、人走路退回来、123321
* · Push()压栈
* · Pop()弹栈 读取并移除顶部对象,注意,移除后数量会跟着减少
* · Peek()读栈顶 读取但不移除顶部对象
* · count 读取栈的统计数量
*
*
*/
private Stack StackVar = new Stack();
private int StackNum;
private void Start4()
{
/*
//压栈 等于列表和字典中的Add
StackVar.Push("你");
StackVar.Push("好");
StackVar.Push("啊");
//读取栈的统计数量
print(StackVar.Count);
//Pop()弹栈 读取并移除顶部对象并通过print打印出来
print(StackVar.Pop());
//Peek()读栈顶 读取但不移除顶部对象
print(StackVar.Peek());
*/
//练习:使用栈练习游戏菜单的返回效果
//主菜单-->选项-->游戏选项-->难度调节-->一般 逐层压栈
//逐层弹栈,最后一个只读不弹
StackVar.Push("主菜单");
StackVar.Push("选项");
StackVar.Push("游戏选项");
StackVar.Push("难度调节");
StackVar.Push("一般");
//注意:for循环不能直接使用StackVar.Count作为栈的长度使用,因为弹栈之后,StackVar.Count就会减一,会出现逻辑错误
//解决办法是使用StackVar.Count赋值给另外一个变量固定住现有的长度数值,在使用这个变量作为for循环的循环条件
StackNum = StackVar.Count;
for (int i = 0; i < StackNum-1; i++)
{
print("弹栈 "+StackVar.Pop());
}
print("最后读栈顶(只读不弹) "+StackVar.Peek());
}
#endregion
#region 队列讲解段-Queue(先进先出) Queue<T>
/*
* 队列 Queue(先进先出) Queue<T> 泛型和非泛型的都是Queue,泛型的需要指定数据类型
* · 先进先出
* · Enqueue () 入队 对应Push()压栈
* · Dequeue () 出对 对应Pop()弹栈 读取并移除队首对象,注意,移除后数量会跟着减少
* · Peek () 读队首 读取但不移除队首对象
* · count 读取队列的统计数量
* · 例子:银行拿号、买票
*/
private Queue QueueVar = new Queue();
private void Start()
{
//入队
QueueVar.Enqueue("man1");
QueueVar.Enqueue("man2");
QueueVar.Enqueue("man3");
QueueVar.Enqueue("man4");
QueueVar.Enqueue("man5");
//出队并打印出对应的值
print(QueueVar.Dequeue());
}
#endregion
}
|