C#核心知识回顾——8.ArryList、Stack栈、队列、哈希表

1.ArryList

ArrayList array = new ArrayList();
        
        //1.增
        array.Add(0);
        array.Add("1");
        array.Add(false);
        ArrayList arrayList = new ArrayList();
        arrayList.Add(123);
        //范围增加(类似于拼接)
        array.AddRange(arrayList);
        //插入(指定位置)
        array.Insert(1, "12345");

        //2.删
        array.Remove(0);//删除第一个找到的指定元素
        //移除指定位置元素
        array.RemoveAt(0);
        //清空
        //array.Clear();

        //3.查
        //得到指定位置元素
        Debug.Log(array[0]);
        //查看元素是否存在
        if (array.Contains(123))
        {
            Debug.Log("存在123");
        }
        //正向查找元素位置(返回的是索引,找不到返回-1)
        Debug.Log(array.IndexOf(false));
        //反向查找
        Debug.Log(array.LastIndexOf(true));

        //4.改
        array[0] = "789";

2.Stack栈:

Stack stack = new Stack();

        //增
        //压栈
        stack.Push(1);
        stack.Push("2");

        //取
        //弹栈
        object obj = stack.Pop();

        //查
        //1.栈无法查看指定位置的元素
        //  只能查看栈顶的内容
        obj = stack.Peek();//只是看看,没有取出
        //2.查看元素是否存在于栈中
        if (stack.Contains("2"))
        {
            Debug.Log("存在2");
        }

        //改
        stack.Clear();

        //遍历
        //1.长度
        //stack.Count
        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stack栈不能使用for循环 !!!!!!!!!!!!!!!!!!!!1
        stack.Push(1);
        stack.Push("2");
        foreach (object item in stack)
        {
            Debug.Log(item);
        }
        //2.将stack转成object数组,即可以for循环
        object[] arry = stack.ToArray();
        for (int i = 0; i < arry.Length; i++)
        {
            Debug.Log(arry[i]);
        }
        //4.循环弹栈(循环完以后,栈就空了)
        while (stack.Count > 0)
        {
            Debug.Log(stack.Pop());
        }

3.队列

 Queue queue = new Queue();

        //增
        queue.Enqueue(1);
        queue.Enqueue("123");
        queue.Enqueue(false);

        //取
        queue.Dequeue();

        //查
        //只看不取
        queue.Peek();

        //改
        queue.Clear();

        //遍历
        queue.Enqueue(1);
        queue.Enqueue("123");
        //长度
        Debug.Log(queue.Count);
        //1.foreach
        foreach (object item in queue)
        {
            Debug.Log(item);
        }
        //2.转为object数组
        object[] obj = queue.ToArray();
        for (int i = 0; i < obj.Length; i++)
        {
            Debug.Log(obj[i]);
        }
        //3.循环出列
        while (queue.Count>0)
        {
            Debug.Log(queue.Dequeue());
        }

4.哈希表

private void Start()
    {
        Hashtable ht = new Hashtable();

        //增
        ht.Add(1, "12");
        ht.Add("11", 2);
        ht.Add(false, false);
        //注意!!不能出现相同的键

        //删
        //只能通过键去删除
        //删除不存在的键没反应
        ht.Remove(1);
        ht.Remove(2);//不存在的键
        //清空
        ht.Clear();

        ht.Add(1, "12");
        ht.Add("11", 2);
        ht.Add(false, false);
        //查
        //通过键查,找不到返回空
        Debug.Log(ht[1]);
        //查看是否存在
        //根据键查询
        if (ht.Contains(1)) { Debug.Log("存在1"); }
        if (ht.ContainsKey(1)) { Debug.Log("存在1"); }
        //根据值查询
        if (ht.ContainsValue("12")) { Debug.Log("存在1"); }


        //改
        ht[1] = 100;

        //遍历        
        //1.遍历键
        foreach (object item in ht.Keys)
        {
            Debug.Log(item);
            Debug.Log(ht[item]);
        }
        //2.遍历值
        foreach (object item in ht.Values)
        {
            Debug.Log(item);
        }
        //3.键值对一起
        foreach (DictionaryEntry item in ht)
        {
            Debug.Log(item.Key + " " + item.Value);
        }
        //4.迭代器
        IDictionaryEnumerator myenu = ht.GetEnumerator();
        bool flag = myenu.MoveNext();
        while (flag)
        {
            Debug.Log(myenu.Key + " " + myenu.Value);
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值