HOME
BLOG
数据结构
9月 06 2022

数组

课程内容:

public class 数组 : MonoBehaviour
{
    // 数组是用来存储数据的集合
    // 1.元素类型相同
    // 2.固定长度
    // 3.顺序集合

    int[] array1;
    int[] array2 = new int[3] { 1,2,3 };
    int[] array3 = { 1,2,3,4,5 };

    private void Start()
    {
        array1 = new int[3];
        Debug.Log(array1[0]);
        array1[0] = 5;
        Debug.Log(array1[0]);

        Debug.Log("-----------------------------------------");

        Debug.Log(array1.Length);

        Debug.Log("-----------------------------------------");

        for (int i = 0; i < array3.Length; i++)
        {
            Debug.Log(array3[i]);
        }

        Debug.Log("-----------------------------------------");

        foreach (var v in array2)
        {
            Debug.Log(v);
        }
    }
}

Arraylist

它代表了可被单独索引的对象的有序集合。

它基本上可以替代一个数组。但是,与数组不同的是,您可以使用索引在指定的位置添加和移除项目,动态数组会自动重新调整它的大小。它也允许在列表中进行动态内存分配、增加、搜索、排序各项。

课程内容:

public class ArrayList1 : MonoBehaviour
{
    // 动态数组( ArrayList ) 对象的有序集合
    // 动态数组会自动重新调整它的大小
    // 可以使用索引在指定的位置添加和移除项目,它也允许在列表中进行动态内存分配

    ArrayList arraylist1 = new ArrayList();
    int[] array1 = new int[4] { 1, 2, 3, 4 };

    void Start()
    {
        arraylist1.Add(45);
        arraylist1.Add(25);
        arraylist1.Add(12);
        //Debug.Log(arraylist1[0]);
        //Debug.Log(arraylist1[1]);
        //Debug.Log(arraylist1[2]);

        // AddRange
        arraylist1.AddRange(array1);

        foreach (var v in arraylist1)
        {
            Debug.Log(v);
        }

        // Clear & Contains
        if (arraylist1.Contains(12))
        {
            Debug.Log("包含元素" + 12);
        }

        //arraylist1.Clear();

        if (arraylist1.Contains(12))
        {
            Debug.Log("包含元素" + 12);
        }

        // IndexOf
        // 1,3,4,12,5,6,12 返回第一个12
        arraylist1[0] = 1;
        arraylist1[1] = 3;
        arraylist1[2] = 4;
        arraylist1[3] = 12;
        arraylist1[4] = 5;
        arraylist1[5] = 6;
        arraylist1[6] = 12;

        Debug.Log( arraylist1.IndexOf(12)); // 没有找到元素的话返回 -1

        // Insert
        Debug.Log("Insert 之前-----");
        foreach (var v in arraylist1)
        {
            Debug.Log(v);
        }

        arraylist1.Insert(3, 66);

        Debug.Log("Insert 之后-----");
        foreach (var v in arraylist1)
        {
            Debug.Log(v);
        }

        // Remove
        arraylist1.Remove(12);

        Debug.Log("Remove 12 之后-----");
        foreach (var v in arraylist1)
        {
            Debug.Log(v);
        }

        // Reverse
        arraylist1.Reverse();

        Debug.Log("Reverse 之后-----");
        foreach (var v in arraylist1)
        {
            Debug.Log(v);
        }

        // Sort
        arraylist1.Sort();

        Debug.Log("Sort 之后-----");
        foreach (var v in arraylist1)
        {
            Debug.Log(v);
        }
    }
}

List

课程内容:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class List1 : MonoBehaviour
{
    // 泛型类集合 List<>
    // 作用与功能近似 arraylist
    // 无需装箱拆箱,类型转换
    // 类型安全

    List<int> list1 = new List<int>();

    void Start()
    {
        list1.Add(1);
        list1.Add(2);
        list1.Add(3);

        //list1.Count;

        //list1.
    }
}

哈希Table

它使用来访问集合中的元素。

当您使用键访问元素时,则使用哈希表,而且您可以识别一个有用的键值。哈希表中的每一项都有一个键/值对。键用于访问集合中的项目。

课程内容:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class 哈希table : MonoBehaviour
{
    // Hashtable 类代表了 基于键的哈希代码组织起来的 键/值对 集合
    // 它使用键来访问集合中的元素

    Hashtable ht1 = new Hashtable();

    void Start()
    {
        ht1.Add("1",100);
        ht1.Add(1,99);
        //ht1.Clear();
        if(ht1.ContainsKey("1"))
        {
            Debug.Log("包含 key 为" + "1" + "的数据");
        }
        ht1.Remove("1");
        Debug.Log(ht1["1"]);
        Debug.Log(ht1[1]);

        ht1[1] = 999;

        ICollection key = ht1.Keys;
        foreach(var k in key)
        {
            Debug.Log(ht1[k]);
        }
    }
}

字典

课程内容:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class 字典Dictionary : MonoBehaviour
{
    // 字典 泛型容器,存储 键值对数据 的集合

    Dictionary<string, string> dic1 = new Dictionary<string, string>();

    void Start()
    {
        dic1.Add("1", "100");
        dic1.Add("2", "200");
        dic1.Add("3", "300");
        if(dic1.ContainsKey("1"))
        {
            Debug.Log("键存在");
        }

        dic1["1"] = "1000";
        foreach (KeyValuePair<string,string> kvp in dic1 )
        {
            Debug.Log(kvp.Key + "" + kvp.Value);
        }

        dic1.Remove("2");
        dic1.Clear();
    }
}

Hashset

课程内容:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HashSet1 : MonoBehaviour
{
    // 包含不重复项的无序列表

    HashSet<int> hs1 = new HashSet<int>();
    HashSet<int> hs2 = new HashSet<int>();

    void Start()
    {
        hs1.Add(1);
        hs1.Add(2);
        hs1.Add(2);
        Debug.Log(hs1.Count);

        hs2.Add(2);
        hs2.Add(3);

        //hs1.IntersectWith(hs2);  // 交集
        //hs1.UnionWith(hs2);      // 并集
        //hs1.ExceptWith(hs2);       // 差集
        hs1.SymmetricExceptWith(hs2); // 对称差集
        foreach (var v in hs1)
        {
            Debug.Log(v);
        }
    }
}

链表

推荐阅读:【Unity3D数据集合】(五)链表LinkedList数据集合学习_恬静的小魔龙的博客-CSDN博客_unity 链表

课程内容:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class 链表 : MonoBehaviour
{
    // 双向链表
    LinkedList<int> linList = new LinkedList<int>();
    LinkedListNode<int> node;

    private void Start()
    {
        node = linList.AddFirst(1);
        linList.AddAfter(node, 2);
        node = linList.AddBefore(node, 0);

        Debug.Log(linList.Count);

        Debug.Log(linList.First.Value);
        Debug.Log(linList.Last.Value);

        if(node.Previous != null)
        {
            Debug.Log(node.Previous.Value);
        }

        if (node.Next != null)
        {
            Debug.Log(node.Next.Value);
        }
    }
    // 链表 list 数组
    // 删除插入效率高于其他,查找速度不如其他
}

堆栈

它代表了一个后进先出的对象集合。

当您需要对各项进行后进先出的访问时,则使用堆栈。当您在列表中添加一项,称为推入元素,当您从列表中移除一项时,称为弹出元素。

课程内容:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class 堆栈 : MonoBehaviour
{
    // Stack 先进后出的对象集合
    Stack st1 = new Stack();
    void Start()
    {
        st1.Push("a");
        st1.Push("b");
        st1.Push("c");
        st1.Push("d");
        Debug.Log(st1.Count);
        string v = (string)st1.Pop(); // 弹出一个
        Debug.Log(v);
        Debug.Log(st1.Count);

        v = (string)st1.Peek(); // 栈顶数据
        Debug.Log(v);
        Debug.Log(st1.Count);

        foreach(var v1 in st1)
        {
            Debug.Log(v1);
        }
    }
}

队列

它代表了一个先进先出的对象集合。

当您需要对各项进行先进先出的访问时,则使用队列。当您在列表中添加一项,称为入队,当您从列表中移除一项时,称为出队

课程内容:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class 队列 : MonoBehaviour
{
    // 先进后出
    Queue queue1 = new Queue();
    Queue<int> queue2 = new Queue<int>();

    void Start()
    {
        queue1.Enqueue("1");
        queue2.Enqueue(1);
        queue2.Enqueue(2);
        queue2.Enqueue(3);

        int v = queue2.Dequeue();
        Debug.Log(v);
        queue2.Enqueue(4);

        foreach (var v1 in queue2)
        {
            Debug.Log(v1);
        }
    }
}

实现堆栈

课程内容:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class 实现栈 : MonoBehaviour
{
    class MyStack
    {
        class StackData
        {
            public StackData nextItem;
            public object topData;
            public StackData(StackData next,object data)
            {
                this.nextItem = next;
                this.topData = data;
            }
        }
        StackData top;
        public void Push(object data)
        {
            top = new StackData(top,data);
        }
        public object Pop()
        {
            object rsl = top.topData;
            top = top.nextItem;
            return rsl;
        }
    }

    private void Start()
    {
        MyStack ms = new MyStack();
        ms.Push(1);
        ms.Push(2);
        ms.Push(3);

        Debug.Log(ms.Pop());
        Debug.Log(ms.Pop());
        Debug.Log(ms.Pop());
    }
}

实现队列

课程内容:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class 实现队列 : MonoBehaviour
{
    class MyQuene
    {
        class QueneData
        {
            public QueneData nextItem;
            public object topData;
            public QueneData(QueneData last, object data)
            {
                last.nextItem = this;
                this.topData = data;
            }
            public QueneData(object data)
            {
                this.topData = data;
            }
        }
        QueneData top;
        QueneData LastData;
        public void Enqueue(object data)
        {
            if (top == null)
            {
                top = new QueneData(data);
                LastData = top;
            }
            else
            {
                LastData = new QueneData(LastData, data);
            }
        }
        public object Dequeue()
        {
            object rsl = top.topData;
            top = top.nextItem;
            return rsl;
        }
    }

    private void Start()
    {
        MyQuene mq1 = new MyQuene();
        mq1.Enqueue(1);
        mq1.Enqueue(2);
        mq1.Enqueue(3);
        mq1.Enqueue(4);
        Debug.Log(mq1.Dequeue());
        Debug.Log(mq1.Dequeue());
        Debug.Log(mq1.Dequeue());
        Debug.Log(mq1.Dequeue());
    }
}

理论知识

集合(Collection)类是专门用于数据存储和检索的类。这些类提供了对栈(stack)、队列(queue)、列表(list)和哈希表(hash table)的支持。大多数集合类实现了相同的接口。

集合(Collection)类服务于不同的目的,如为元素动态分配内存,基于索引访问列表项等等。这些类创建 Object 类的对象的集合。在 C# 中,Object 类是所有数据类型的基类。

推荐阅读 - 各种集合类和它们的用法:

1.C# 动态数组(ArrayList) | 菜鸟教程

2.C# 哈希表(Hashtable) | 菜鸟教程

3.C# 排序列表(SortedList) | 菜鸟教程

4.C# 堆栈(Stack) | 菜鸟教程

5.C# 队列(Queue) | 菜鸟教程

6.C# 点阵列(BitArray) | 菜鸟教程



👾 - Morikiiii - 👾

C# Study