集合相关

列表是动态数组,是有序的即输入和存储顺序一致

字典是无序的(元素个数多了就能体现出来)

列表

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]);
            }
        }
    }
}
View Code

字典

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["流星锤"]);

    }
}
View Code
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]);
        }
    }
}
View Code

队列

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

堆栈

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());
    }
}
View Code

 

转载于:https://www.cnblogs.com/hyit/articles/5990125.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值