Unity中List常用的增删改查和对比操作

一、对比:

 #region 对比
    //对比相等(顺序也要相同)
    private void Test1()
    {
        List<int> listA1 = new List<int> { 1, 2, 3, 4, 5, 6 };
        List<int> listA2 = new List<int> { 1, 2, 3, 4, 5, 6 };

        //SequenceEqual方法需引用using System.Linq;
        if (listA1.SequenceEqual(listA2))
        {
            Debug.Log("A1==A2");
        }
        else
        {
            Debug.Log("A1!=A2");
        }

        List<int> listB1 = new List<int> { 1, 2, 2, 4, 5, 6 };
        List<int> listB2 = new List<int> { 1, 2, 4, 4, 5, 6 };
        if (listB1.SequenceEqual(listB2))
        {
            Debug.Log("B1==B2");
        }
        else
        {
            Debug.Log("B1!=B2");
        }

        List<int> listC1 = new List<int> { 1, 2, 3, 4, 5, 6 };
        List<int> listC2 = new List<int> { 1, 3, 2, 5, 4, 6 };
        if (listB1.SequenceEqual(listB2))
        {
            Debug.Log("C1==C2");
        }
        else
        {
            Debug.Log("C1!=C2");
        }
    }

    //对比元素相同(可乱序)
    private void Test2()
    {
        List<int> listA1 = new List<int> { 1, 1, 2, 2, 3, 3 };
        List<int> listA2 = new List<int> { 2, 2, 1, 1, 3, 3 };
        if (IsEqual(listA1, listA2))
        {
            Debug.Log("listA1和listA2所含元素相同");
        }
        else
        {
            Debug.Log("listA1和listA2所含元素不相同");
        }

        List<int> listB1 = new List<int> { 1, 1, 2, 2, 3, 3 };
        List<int> listB2 = new List<int> { 1, 2, 2, 2, 3, 3 };
        if (IsEqual(listB1, listB2))
        {
            Debug.Log("listB1和listB2所含元素相同");
        }
        else
        {
            Debug.Log("listB1和listB2所含元素不相同");
        }

    }
    private bool IsEqual<T>(List<T> list1, List<T> list2)
    {
        return new HashSet<T>(list1).SetEquals(list2);
    }
    #endregion

二、增加元素

  #region 增加

    //在指定下标后增加一个元素
    private void Test3()
    {
        List<string> list1 = new List<string> { "a", "b", "d", "e", "f" };
        List<string> list2 = new List<string>();
        //在index=1即元素"b"后面后增加一个c
        int indexTemp = 1;
        for (int i = 0; i < list1.Count + 1; i++)
        {
            if (i < indexTemp + 1)
            {
                list2.Add(list1[i]);
            }
            else if (i == indexTemp + 1)
            {
                list2.Add("c");
            }
            else
            {
                list2.Add(list1[i - 1]);
            }
        }

        //这个循环只是用来打印结果,无关功能实现
        Debug.Log("操作完成后的list为:");
        for (int i = 0; i < list2.Count; i++)
        {
            Debug.Log("list2[" + i + "]=" + list2[i]);
        }
    }

    //在指定元素后增加一个元素
    private void Test4()
    {
        List<string> list1 = new List<string> { "a", "b", "d", "e", "f" };
        List<string> list2 = new List<string>();
        //在b和d中间增加c
        int indexTemp = 0;
        for (int i = 0; i < list1.Count; i++)
        {
            if (list1[i] == "b")
            {
                indexTemp = i;
                break;
            }
        }
        for (int i = 0; i < list1.Count + 1; i++)
        {
            if (i < indexTemp + 1)
            {
                list2.Add(list1[i]);
            }
            else if (i == indexTemp + 1)
            {
                list2.Add("c");
            }
            else
            {
                list2.Add(list1[i - 1]);
            }
        }

        //这个循环只是用来打印结果,无关功能实现
        Debug.Log("操作完成后的list为:");
        for (int i = 0; i < list2.Count; i++)
        {
            Debug.Log("list2[" + i + "]=" + list2[i]);
        }
    }

    #endregion

三、删除元素

 #region 删除

    //移除指定元素
    private void Test5()
    {
        //原list
        List<string> list1 = new List<string> { "1", "2", "3", "4", "5", "6" };
        //所有需要移除的元素
        List<string> list2 = new List<string> { "2", "5", "1" };

        foreach (string item in list2)
        {
            list1.Remove(item);
        }
        //这个循环只是用来打印结果,无关功能实现
        Debug.Log("操作完成后的list为:");
        for (int i = 0; i < list1.Count; i++)
        {
            Debug.Log("list1[" + i + "]=" + list1[i]);
        }
    }

    //随机移除指定个数元素
    private void Test6()
    {
        List<string> list1 = new List<string> { "a", "b", "c", "d", "e", "f" };
        int count = 3;
        for (int i = 0; i < count; i++)
        {
            int indexTemp = Random.Range(0, list1.Count - 1);
            Debug.Log("第" + i + "轮移除的是:" + list1[indexTemp]);
            list1.RemoveAt(indexTemp);
        }

        //这个循环只是用来打印结果,无关功能实现
        Debug.Log("操作完成后的list为:");
        for (int i = 0; i < list1.Count; i++)
        {
            Debug.Log("list1[" + i + "]=" + list1[i]);
        }

    }

    #endregion

四、修改元素

#region 修改
    //修改指定下标的元素
    private void Test7()
    {
        List<string> list1 = new List<string> { "f", "uc", "k" };
        //要修改的元素下标为1
        int indexTemp = 1;
        for (int i = 0; i < list1.Count; i++)
        {
            if (i == indexTemp)
            {
                list1[i] = "**";
            }
        }

        //这个循环只是用来打印结果,无关功能实现
        Debug.Log("操作完成后的list为:");
        for (int i = 0; i < list1.Count; i++)
        {
            Debug.Log("list1[" + i + "]=" + list1[i]);
        }
    }

    //修改指定元素
    private void Test8()
    {
        List<string> list1 = new List<string> { "f", "uc", "k", "d", "uc", "k" };
        //要修改的元素为"uc"
        for (int i = 0; i < list1.Count; i++)
        {
            if (list1[i] == "uc")
            {
                list1[i] = "**";
            }
        }

        //这个循环只是用来打印结果,无关功能实现
        Debug.Log("操作完成后的list为:");
        for (int i = 0; i < list1.Count; i++)
        {
            Debug.LogError("listF1[" + i + "]=" + list1[i]);
        }
    }

    #endregion

五、查找

    #region 查找
    //查找指定元素对应的下标
    private void Test9()
    {
        List<string> listI1 = new List<string> { "a","b","c","a","d" };
        //找所有"a"对应的下标
        for (int i = 0; i < listI1.Count; i++)
        {
            if (listI1[i]=="a")
            {
                Debug.Log(i);
            }
        }
    }

    #endregion

猜你喜欢

转载自blog.csdn.net/weixin_43818160/article/details/126099464