page contents

Array和ArrayList有什么区别?

Nen 发布于 2022-11-09 16:30
阅读 905
收藏 0
分类:C/C++开发
4395
王昭君
王昭君

Array和 ArrayList 类似。当你想存储相同类型的项目时,可以使用Array。Array具有固定大小。当你要存储任何类型的数据时,我们使用 ArrayList。ArrayList 没有固定大小。参考数组和ArrayList的例子:


using System.Collections;
namespace demoapp
{
    class Sample
    {
        //Array and Araraylist.
        public void ArrayFunction()
        {
            string[] country = new string[3];
            country[0] = "USA"//only string value can be added
            country[1] = "Denmark";
            country[2] = "Russia";

            //can store different data types
            ArrayList arraylist = new ArrayList();
            arraylist.Add(3);
            arraylist.Add("USA");
            arraylist.Add(false);
        }
    }
}


请先 登录 后评论