数组
关键词:长度固定,顺序
以下代码演示内容
public class BasicDatatype : MonoBehaviour
{
//声明和初始化的3种方式
int[] array1 = new int[3] { 1, 4, 5 };
int[] array2;
int[] array3 = { 4, 6 };
// Start is called before the first frame update
void Start()
{
array2 = new int[6];//没有赋值的时候默认所有元素都是0;可以不赋值但是不能不指定长度
//以下展示数组遍历
for(int i=0; i<array1.Length; i++)
{
Debug.Log(array1[i]);
}
}
}
ArrayList
关键词:对象的有序集合、动态数组
对象是object类型,需要装箱和拆箱
动态意味着不用固定长度,会自动调整大小
以下代码演示的内容:
- 声明和初始化
- 增删改查
- 常见属性:Count、Contains、IndexOf
- 常见方法:Insert、Remove、Clear、Reverse、Sort
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BasicDatatype : MonoBehaviour
{
//这是声明和初始化
ArrayList arrayList = new ArrayList();
int[] array = { 1, 7, 8 };
string[] str = { "kk", "pp" };
// Start is called before the first frame update
void Start()
{
//为动态数组添加单个元素
arrayList.Add(34);
arrayList.Add(18);
arrayList.Add(25);
//添加一个数组
arrayList.AddRange(array);
arrayList.AddRange(str);
//修改一个元素
arrayList[1] = 100;
//遍历动态数组
foreach(var v in arrayList)
{
Debug.Log(v);
}
//一些其他的常用属性
Debug.Log(arrayList.Count);//返回长度
print(arrayList.Contains(12));//返回布尔值
Debug.Log(arrayList.IndexOf(25));//返回元素的下标,这里应该返回2
Debug.Log(arrayList.IndexOf(2));//没有的话应该返回-1
//一些其他常用的方法
arrayList.Insert(1, 12);//在arraylist的第1位后面插入12
arrayList.Insert(0, "star");
arrayList.Remove(12);//第一次遇到的12将会被删掉;如果不包含这个值就没有作用
arrayList.Reverse();//逆转
//arrayList.Sort();//进行排序,但是如果前面的过程中没有控制好数据类型,比如我们这里加入了字符串,排序就会出错
arrayList.Clear();//清除其内容
}
}
List
关键词:泛型类集合
类似ArrayList,但是无需装箱和拆箱,类型安全
public class BasicDatatype : MonoBehaviour
{
List<int> list = new List<int>();
void Start()
{
list.Add(2);
list.Add(7);
Debug.Log(list.Count);
//很多内容都和arraylist很像,但是因为类型安全,所以更推荐用L
}
}
HashTable 哈希表
关键词:键值对
用object类型,和ArrayList一样,类型不安全
以下代码演示的内容:
- 声明和初始化
- 增删改查
- 常见属性:Count、Contains、ContainsKey
- 常见方法:Remove、Clear
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BasicDatatype : MonoBehaviour
{
Hashtable ht = new Hashtable();
void Start()
{
//增
ht.Add("1", 100);
ht.Add("2", 300);
ht.Add(3, 800);
//删
ht.Remove(3);//清除键为3的键值对
ht.Clear();//全部清除
//改
ht["1"] = 900;
//查
Debug.Log(ht["1"]);
//获得所有的键
ICollection key = ht.Keys;
//遍历
foreach(var v in key)
{
Debug.Log(ht[v]);
}
//以下是属性
Debug.Log(ht.Count);//长度
Debug.Log(ht.ContainsKey("1"));//按键查找,返回布尔值
Debug.Log(ht.Contains(100));//按值查找,返回布尔值
}
}
Dictionary字典
关键词:用泛型类做键值对
以下两对关系很像:
- HashTable(object类型,类型不安全)——>Dictionary(泛型,类型安全)
- ArrayList(object类型,类型不安全)——>List(泛型,类型安全)
以下代码演示的内容:
- 声明和初始化
- 增删改查
- 常见属性:Count、Contains、ContainsKey
- 常见方法:Remove、Clear
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BasicDatatype : MonoBehaviour
{
Dictionary<string, string> dic = new Dictionary<string, string>();
void Start()
{
//增
dic.Add("1", "200");
dic.Add("3", "600");
//删
dic.Remove("3");
dic.Clear();
//改
dic["3"] = "kk";
//查
Debug.Log(dic["3"]);
//常用属性
Debug.Log(dic.ContainsKey("1"));
//遍历
foreach(KeyValuePair<string, string> kvp in dic)
{
Debug.Log(kvp.Key + ":" + kvp.Value);
}
}
}
HashSet
关键词:不能重复的序列表
以下代码演示的内容:
- 声明和初始化
- 增删改查
- 两个集合之间的运算
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BasicDatatype : MonoBehaviour
{
HashSet<int> hs1 = new HashSet<int>();
HashSet<string> hs2 = new HashSet<string>();
HashSet<int> hs3 = new HashSet<int>();
void Start()
{
//增加
hs1.Add(1);
hs1.Add(2);
hs1.Add(2);
//计数
Debug.Log(hs1.Count);
hs3.Add(1);
hs3.Add(3);
hs3.Add(3);
//两个集合之间的运算
hs1.IntersectWith(hs3);//返回值是交集的部分
hs1.UnionWith(hs3);
hs1.ExceptWith(hs3);
hs1.SymmetricExceptWith(hs3);
//遍历
foreach (var v in hs1)
{
Debug.Log(v);
}
//没有自带的排序,想要排序只能走List
}
}
ListNode链表
关键词:泛型
- list、数组的插入和删除需要移位,但是索引方便,不用遍历
- 链表的插入和删除不用移位,但是索引的时候需要遍历
以下代码演示主要的代码功能:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BasicDatatype : MonoBehaviour
{
LinkedList<int> linkList = new LinkedList<int>();//声明一个链表
LinkedListNode<int> node;//声明一个节点
LinkedListNode<int> next;
void Start()
{
node = linkList.AddFirst(2);
linkList.AddAfter(node, 6);//在后面加
node = linkList.AddBefore(node, 1);//在前面加
next = linkList.AddAfter(node, 2);
//查
Debug.Log(linkList.Count);
Debug.Log(linkList.First.Value);
Debug.Log(linkList.Last.Value);
if (node.Previous != null)
Debug.Log(node.Previous.Value);
if (node.Next != null)
Debug.Log(node.Next.Value);
//插入
next = linkList.AddAfter(node, 3);
Debug.Log(node.Next.Value);
}
}
Stack
关键词:先进后出
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BasicDatatype : MonoBehaviour
{
Stack st = new Stack();
void Start()
{
st.Push("a");
st.Push("b");
st.Push("c");
object top = st.Pop();//出栈
object peek = st.Peek();//拿到栈顶的元素
Debug.Log(top.ToString());
Debug.Log(peek.ToString());
Debug.Log(st.Count);//拿到数量
//遍历
foreach(var v in st)
{
Debug.Log(v);
}
}
}
Queue
关键词:先进先出
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BasicDatatype : MonoBehaviour
{
Queue queue1 = new Queue();//object类型
Queue<int> queue2 = new Queue<int>();//泛型类
void Start()
{
//增
queue1.Enqueue("kkk");
queue1.Enqueue(3);
//删
Debug.Log(queue1.Dequeue());//输出kkk,也会改变queue1的值
//遍历
foreach (var v in queue1)
{
Debug.Log(v);
}
//其他的方法:Count/Contains/Clear也都有
}
}