unity中 List、Dictionary和Queue的使用学习记录

记录在unity中,C#中列表、字典、队列的使用和对比。

using System.Collections;
using System.Collections.Generic;   //命名空间是必须的
using UnityEngine;

//list常用(简单)使用记录
public class listTest : MonoBehaviour {
    //添加: .Add(value)
    //查找:.FindIndex() 按值返回索引
    //是否存在:.Contains(value)
    //.Find()
    //.Exists()
    //按内容移除:  .Remove(mPos[index]);
    //按索引移除: .mPos.RemoveAt(index);
    //清空: .Clear();
    //注意:移除方式不同,方法不同
    //判断是否存在,方法不同,返回不同
    //必要时候需要判断数量

    public List<Vector3> myPosList = new List<Vector3>();

    // Use this for initialization
    void Start ()
    {

    }
	
	// Update is called once per frame
	void Update ()
    {
        if (Input.GetMouseButtonDown(0))
        {//添加
            int num = Random.Range(1,10);

            myPosList.Add(new Vector3(0,0, num));
            Debug.Log("num = " + num + " 添加后 myPosList count " + myPosList.Count);
        }

        if (Input.GetMouseButtonDown(1) && myPosList.Count > 0)
        {//删除,两种方法都行
            int num = Random.Range(0, myPosList.Count);

            //myPosList.RemoveAt(num);//移除方法
            myPosList.Remove(myPosList[num]); 
            Debug.Log("num = " + num + " 删除后 myPosList count " + myPosList.Count);

            if (myPosList.Contains(new Vector3(0,0,1)))
            {
                Debug.Log("存在 (0,0,1)");
            }
            else
            {
                Debug.Log("不存在 (0,0,1)");
            }

            Vector3 v1 = myPosList.Find(e => e == new Vector3(0, 0, 2));
            //这个测试结果是,如果存在,返回。不存在返回(0,0,0)。具体情况具体看吧
            if (v1 != Vector3.zero)
            {
                Debug.Log("Find存在 (0,0,2)  " + v1);
            }
            else
            {
                Debug.Log("Find不存在 (0,0,2)  " + v1);
            }

            bool v2 = myPosList.Exists(e => e == new Vector3(0, 0, 2));

            if (v2)
            {
                int index = myPosList.FindIndex(item => item.Equals(new Vector3(0, 0, 2)));
                Debug.Log("Exists存在 (0,0,2)  " + v2 + " index= " + index);
            }
            else
            {
                Debug.Log("Exists不存在 (0,0,2)  " + v2);
            }

        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            //遍历
            for (int i = 0; i < myPosList.Count; i++)
            {
                Debug.Log(i + " "+ myPosList[i]);
            }
        }
    }
}
using System.Collections;
using System.Collections.Generic;   //是必须的
using UnityEngine;

//字典常用(简单)使用记录
public class dictionaryTest : MonoBehaviour {
    //字典Dictionary<TKey, TValue>    TKey唯一
    //如果需要按键对保存,按key直接查找,可以使用字典
    //添加: .Add(key, value)
    //删除: .Remove(key);
    //查找: foreach
    //获取:myDic[key]
    //数量: .Keys.Count
    //修改: .myDic[key] = value;
    //注意:先查找key是否存在(使用的时候吧)
    //判断key是否存在:.ContainsKey(key)
    //判断value是否存在:.ContainsValue(value)

    public Dictionary<string, int> myDic = new Dictionary<string, int>();

    // Use this for initialization
    void Start ()
    {//比如存放姓名和年龄
        //添加
        myDic.Add("张二三", 20);
        myDic.Add("李四五", 15);//给字典添加数据
        //myDic.Add("李四五", 25);//给字典添加数据——key值重复,报错
        myDic.Add("王哈哈", 32);

        //获取长度
        Debug.Log(" dic count = "+ myDic.Keys.Count);//读取当前字典长度

        //删除——按key值
        if (myDic.ContainsKey("张二三"))
        myDic.Remove("张二三");
        myDic.Remove("张二一三");//这个不存在,删除时候没报错。

        //修改
        myDic["李四五"] = 26;

        //获取value方法1
        int age1;
        myDic.TryGetValue("李四五", out age1);//从字典里读取数据
        Debug.Log("李四五TryGetValue()  " + age1);

        //获取value方法2
        int age2;
        age2 = myDic["李四五"];//也可以这样获取数据
        Debug.Log("李四五myDic[key]  " + age2);

        //遍历key - value
        foreach (KeyValuePair<string, int> pair in myDic)
        {
            Debug.Log("name & age value = " + pair.Key + " "+ pair.Value);
        }

        //遍历key
        foreach (string keyTemp in myDic.Keys)
        {
            Debug.Log("name(key) value = " + keyTemp);
        }

        //遍历value
        foreach (int valueTemp in myDic.Values)
        {
            Debug.Log("age value = "+ valueTemp);
        }

        //另外一个字典
        Dictionary<int, string> myDic1 = new Dictionary<int, string>();
        myDic1.Add(0, "name");
        myDic1.Add(1, "age");
        myDic1.Remove(0);
        myDic1.Remove(2);
    }
}
using System.Collections;
using System.Collections.Generic;   //是必须的
using UnityEngine;

//队列常用(简单)使用记录
public class queueTest : MonoBehaviour {
    //队列使用记录(如果是一直存----获取第一个并删除,可以使用队列)
    //.Enqueue(参数)——给队列添加
    //.Dequeue()——没有参数,获取第一个值并删除
    //.Peek()——只是读取,不删除
    //.Count_——获取数量

    public Queue<int> myQuene;

    private void Start()
    {
        myQuene = new Queue<int>();

        //添加
        myQuene.Enqueue(1);
        myQuene.Enqueue(2);
        myQuene.Enqueue(3);

        Debug.Log("myQuene Count = " + myQuene.Count);
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0) && myQuene.Count > 0)
        {
            Debug.Log("读取并删除.Dequeue() " + myQuene.Dequeue());
        }

        if (Input.GetMouseButtonDown(1) && myQuene.Count > 0)
        {
            Debug.Log("读取不删除.Peek() " + myQuene.Peek());
        }
    }

    //注意事项:判断队列数量
    void TestFun()
    {
        if (myQuene.Count > 0 && myQuene.Peek() == 1)//注意:使用判断时候,用读取不删除.Peek(),以免被误删
        {
            //do
            UseValue(myQuene.Dequeue());    //使用第一个值,并删除
        }
        else if (myQuene.Count > 0 && myQuene.Peek() == 2)
        {
            //do
            UseValue(myQuene.Dequeue());    //使用第一个值,并删除
        }
    }

    void UseValue(int num)
    { }
}

 描述在代码注释和图片中有。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值