CSharp学习笔记之二 C#中的数组

所谓的数组就是具有相同的数据类型且按一定次序排列的一组变量的集合体,构成一个数组的这些变量称为数组元素。其实数组并不仅仅局限于存在在C#中,在C、C++、java等语言中也都存在着数组。

对于数组首先我们来看一个例子,就是关于数组的创建:

class TestArraysClass
{
    static void Main()
    {
        // 定义一个一维的整形数组,其中的数组的有5个数组元素 
        int[] array1 = new int[5];

        // 定义一个一维数组,数组中的元素个数有5个,分别为:1, 3, 5, 7, 9
        int[] array2 = new int[] { 1, 3, 5, 7, 9 };

        // 这个是数组的另外的一种初始化的方式
        int[] array3 = { 1, 2, 3, 4, 5, 6 };

        // 定义一个二维的2行3列的数组整形数组
        int[,] multiDimensionalArray1 = new int[2, 3];

        int[,] multiDimensionalArray2 = { { 1, 2, 3 }, { 4, 5, 6 } };

        //  定义一个交错数组,也成为数组的数组
        int[][] jaggedArray = new int[6][];

        // 
        jaggedArray[0] = new int[4] { 1, 2, 3, 4 };
    }
}

在上面的例子中演示了有关数组的定义:

如果要获得数组的长度的话所选用的方法就是用   :
  int   count = numberArrayy.Length;
其中的这个Length是数组中的一个默认的属性
如果要使用数组中的的数据的时候可以通过这样的一个方法来对数据中的数据进行使用
numberArray[index],其中的index是数组中的元素的下标。其下标的范围是:0 ~ count -1;
如果要对数组中的数据进行遍历使用的话,这里面有两种方法:
 
方法一:
for(int i = 0; i < numebrArray.Lenght; i++
{
     Console.WriteLine("{0}", numberArray[
}
方法二:
foreach(int number in numberArrary)
{
     Console.WriteLinr("{0}", number);
}
 
对于多维数组的和交错数组的区别:
其实多维数组完全不等于交错数组的,首先从定义上面来讲:
对于多维数组的的地定义我们可以用如下的方法进行定义和赋值:
// Two-dimensional array.
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// The same array with dimensions specified.
int[,] array2Da = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// A similar array with string elements.
string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" },
                                        { "five", "six" } };

// Three-dimensional array.
int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
                                 { { 7, 8, 9 }, { 10, 11, 12 } } };
// The same array with dimensions specified.
int[, ,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
                                       { { 7, 8, 9 }, { 10, 11, 12 } } };

// Accessing array elements.
System.Console.WriteLine(array2D[0, 0]);
System.Console.WriteLine(array2D[0, 1]);
System.Console.WriteLine(array2D[1, 0]);
System.Console.WriteLine(array2D[1, 1]);
System.Console.WriteLine(array2D[3, 0]);
System.Console.WriteLine(array2Db[1, 0]);
System.Console.WriteLine(array3Da[1, 0, 1]);
System.Console.WriteLine(array3D[1, 1, 2]);

// Output:
// 1
// 2
// 3
// 4
// 7
// three
// 8
// 12
而对于交错数组的定义和赋值则可以通过如下的方法来进行赋值的:
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];
// 或者如下的方法进行赋值

jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };
jaggedArray[1] = new int[] { 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 11, 22 };

// 或者如下的方法

    int[][] jaggedArray2 = new int[][] 
{
    new int[] {1,3,5,7,9},
    new int[] {0,2,4,6},
    new int[] {11,22}
};


    int[][] jaggedArray3 = 
{
    new int[] {1,3,5,7,9},
    new int[] {0,2,4,6},
    new int[] {11,22}
};
 

数组具有以下属性:                       

  • 数组可以是一维、多维或交错的。                               

  • 当创建了数组实例时,将建立维度数和每个维度的长度。                  

  • 在实例的生存期内,这些值不能更改。 

  • 数值数组元素的默认值设置为零,而引用元素的默认值设置为 null。                              

  • 交错数组是数组的数组,因此其元素是引用类型并初始化为 null。                              

  • 数组的索引从零开始:具有 n 个元素的数组的索引是从0n-1              

  • 数组元素可以是任何类型,包括数组类型。                               

  • 数组类型是从抽象基类型 Array派生的引用类型。                 

  • 由于此类型实现了 IEnumerable 和 IEnumerable<T>,因此可以对 C# 中的所有数组使用 foreach 迭代

数组的主要是用来存储一组数据的,数组的使用会大大的节省编码的时间,对于数组的使用这里要深入体会和理解数组在内存中的存储结构等东西。


 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值