牛骨文教育服务平台(让学习变的简单)
博文笔记

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

创建时间:2017-03-07 投稿人: 浏览次数:3479

如果有很多同等类型的 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,并且长度不能超过5
ArrayList-->集合的一种,其中可以放任何类型,不受限制,长度可变,自增加长度
例如: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.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。