Esempio n. 1
0
    void TestArray()
    {
        var sb = new StringBuilder();

        Debug.LogError("##########TestArray##########");
        CollectionItem[] array = new CollectionItem[5];
        array[0] = new CollectionItem {
            name = "Petter", id = 1
        };
        array[1] = new CollectionItem {
            name = "Andy", id = 2
        };
        array[2] = new CollectionItem {
            name = "Bob", id = 3
        };
        array[3] = new CollectionItem {
            name = "George", id = 4
        };
        array[4] = new CollectionItem {
            name = "Kelly", id = 5
        };

        Debug.Log("=======CopyTo=======");
        var array1 = new CollectionItem[10];

        array.CopyTo(array1, 1);
        foreach (var item in array1)
        {
            if (item != null)
            {
                sb.AppendLine(item.ToString());
            }
            else
            {
                sb.AppendLine("item is Null");
            }
        }
        Debug.Log(sb.ToString());
        sb.Length = 0;

        Debug.Log("=======Array.Copy=======");
        var array2 = new CollectionItem[10];

        System.Array.Copy(array, array2, 3);
        foreach (var item in array2)
        {
            if (item != null)
            {
                sb.AppendLine(item.ToString());
            }
            else
            {
                sb.AppendLine("item is Null");
            }
        }
        Debug.Log(sb.ToString());
        sb.Length = 0;

        Debug.Log("=======ConvertAll=======");
        string[] nameArray = System.Array.ConvertAll(array, (item) =>
        {
            return(item.name);
        });
        foreach (var item in nameArray)
        {
            if (item != null)
            {
                sb.AppendLine(item.ToString());
            }
            else
            {
                sb.AppendLine("item is Null");
            }
        }
        Debug.Log(sb.ToString());
        sb.Length = 0;

        Debug.Log("=======Int32 Array CopyTo=======");
        var ints1 = new int[8];
        var ints2 = new int[] { -1, 3, 0, 7 };

        ints2.CopyTo(ints1, 0);
        sb.Append("ints1:");
        foreach (int i in ints1)
        {
            sb.Append(i + ",");
        }
        sb.AppendLine();
        sb.Append("ints2:");
        foreach (int i in ints2)
        {
            sb.Append(i + ",");
        }
        sb.AppendLine();
        Debug.Log(sb.ToString());
        sb.Length = 0;

        Debug.Log("=======Float32 Array CopyTo=======");
        var floats1 = new float[8];
        var floats2 = new float[] { -1.89f, 3.14f, 0f, 0.4f };

        floats2.CopyTo(floats1, 0);
        sb.Append("floats1:");
        foreach (float i in floats1)
        {
            sb.Append(i + ",");
        }
        sb.AppendLine();
        sb.Append("floats2:");
        foreach (float i in floats2)
        {
            sb.Append(i + ",");
        }
        sb.AppendLine();
        Debug.Log(sb.ToString());
        sb.Length = 0;
    }