码迷,mamicode.com
首页 > 编程语言 > 详细

第2章.数组和ArrayList

时间:2019-07-19 18:47:00      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:html   ace   set   sharp   arrays   setvalue   对象   sys   The   

2.1 数组基本概念

数组是可索引的数据的集合。数组既可以是内置的类型,也可以是用户自定义的类型。事实上,把数组数据称为对象大概是最简便的方式。C#中数组实际上就是对象的本身,因为它们都源于System.Array类的一个声明实例,所以在使用数组时也可以使用此类的所有方法和属性。

2.1.1 数组的声明和初始化

技术图片
namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化数组需要确定数组大小 此处预留了5个字符串的内存空间
            string[] names = new string[10];

            //也可以采用初始化列表的方式来实现 无需指定元素个数
            int[] numbers = new int[] { 1, 2, 3, 4, 5 };
        }
    }
}
View Code

2.1.2 数组元素的设置和存取访问

技术图片
namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] nNames = new string[10];

            //存储数组元素既可以采用直接存取访问的方法也可以通过调用Array类的SetValue方法

            //直接存取方式通过赋值语句左侧的索引来引用数组位置
            nNames[2] = "Raymond";

            //SetValue方法会取走两个参数 一个是索引数另一个是元素值
            nNames.SetValue("Raymond", 2);

            //数组元素访问原理同上
            string s1 = nNames[2];
            string s2 = nNames.GetValue(2) as string;
        }
    }
}
View Code

2.1.3 取回数组元数据的方法和属性

技术图片
namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            //Array类为取回数组元素数据提供了几种属性:
            //Length:返回数组所有维数内元素的总数量
            //GetLength:返回数组指定维数内元素的数量
            //Rank:返回数组的维数
            //GetType:返回当前数组实例的类型

            int[] numbers = new int[] { 0, 1, 2, 3, 4 };
            Type arrayType = numbers.GetType();
            if (arrayType.IsArray)
            {
                Console.WriteLine("The array type is: {0}", arrayType);
            }
            else
            {
                Console.WriteLine("Not an array");
            }
            Console.Read();
        }
    }
}
View Code

2.1.4 多维数组

参考链接:https://www.runoob.com/csharp/csharp-multi-dimensional-arrays.html

2.1.5 参数数组

参考链接:https://www.runoob.com/csharp/csharp-param-arrays.html

2.1.6 锯齿状数组

参考链接:https://www.runoob.com/csharp/csharp-jagged-arrays.html

2.2 ArrayList类

 

第2章.数组和ArrayList

标签:html   ace   set   sharp   arrays   setvalue   对象   sys   The   

原文地址:https://www.cnblogs.com/huangxuQaQ/p/11214877.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!