【Unity&C#&数组】如何创建一个存放任何类型变量的数组

来源:互联网 发布:js for循环给数组赋值 编辑:程序博客网 时间:2024/06/08 10:16

如果有很多同等类型的 Int 类型(其他任何类型都可以)的 变量,想统一的对其进行操作怎么办

-------------------------------------------------------------------------------

1. ArrayList 的用法参考资料1

举个栗子


//声明
    private int Num_1 = 1;
    private int Num_2 = 2;
    private int Num_3 = 3;
    private int Num_4 = 4;
    private int Num_5 = 5;

private ArrayList List = new ArrayList(5);//ArrayList 默认是4个长度,使用之前记得 给予一个长度


    // Use this for initialization初始化
    void Start () {
        List.Add(Num_1);
        List.Add(Num_2);
        List.Add(Num_3);
        List.Add(Num_4);
        List.Add(Num_5);

ShowList();

   }//


    void ShowList()//使用函数对其调用
    {
        for (int i=0; i<List.Count ;i++)
        {
            Debug.Log("5555555555"+List[i]);
        }
    }


-------------------------------------------------------------------------------

如果对ArrayList里面的一个数组赋值,后果会怎么样

结果并没有改变,也就是说 直接 改变 ArrayList 的成员的变量,ArrayList 的对应成员 并没有改变数值,当其成员变量 的值发生改变,需要更新ArrayList 才能实时显示。

-------------------------------------------------------------------------------

2. 数组 参考资料2

字符串型string整型 int 浮点型 float ,double,GameObject,Transform(作者没有试过,可以尝试一下),等类型

-------------------------------------------------------------------------------

3. List<T>用法参考资料3

字符串型string整型 int 浮点型 float ,double,GameObject,Transform(作者没有试过,可以尝试一下),等类型

-------------------------------------------------------------------------------

三者的区别:参考资料6

int[]--->数组,只能放int类型的元素,并且必须定长度例如:int[] T=new int[5]; 只能放int,并且长度不能超过5ArrayList-->集合的一种,其中可以放任何类型,不受限制,长度可变,自增加长度例如:ArrayList AR=new ArrayList(){"你好",0,new int[5]{1,2,3,4,5},User}; 可以放任意类型List<T>--->集合的一种,其中只能放相同类型的数据,长度可变,增长度例如:List<int> list=new list<int>(){0,1,1,2,3,4},只能放int,



参考资料:

1.

ArrayList的用法

2.

【Unity&对象数组】如何创建一个长度不确定的对象数组以及使用方法&墙壁单元方块对象数组

3.

Unity3D C# List数据类型使用

4.

Unity3D中可用的数组类型:

5.

C#中ArrayList.ToArray()用法

6.

C#中数组,ArrayList和List三者的区别Unity3d

7.

8.

9.

10.

11.

0 0
原创粉丝点击