zoukankan      html  css  js  c++  java
  • C#数组

    C# 数组从零开始建立索引,即数组索引从零开始。

    1、声明数组时,方括号 ([]) 必须跟在类型后面,而不是标识符后面。

    int[] table; // not int table[]; 

    2、数组的大小不是其类型的一部分,可以声明一个数组并向它分配 int 对象的任意数组,而不管数组长度如何。

    int[] numbers; // declare numbers as an int array of any size
    numbers = new int[10];  // numbers is a 10-element array
    numbers = new int[20];  // now it's a 20-element array

    3、C# 支持一维数组、多维数组(矩形数组)和数组的数组(交错的数组)。

    一维数组:int[] numbers;

    多维数组:string[,] names;

    数组的数组(交错的):byte[][] scores;

    4、声明数组(如上所示)并不实际创建它们。在 C# 中,数组是对象(本教程稍后讨论),必须进行实例化。下面的示例展示如何创建数组:

    一维数组:int[] numbers = new int[5];

    多维数组:string[,] names = new string[5,4];

    数组的数组(交错的):

    byte[][] scores = new byte[5][];
    for (int x = 0; x < scores.Length; x++) 
    {
       scores[x] = new byte[4];
    }
    

    还可以有更大的数组。例如,可以有三维的矩形数组:

    int[,,] buttons = new int[4,5,3];
    

    甚至可以将矩形数组和交错数组混合使用。例如,下面的代码声明了类型为 int 的二维数组的三维数组的一维数组。

    int[][,,][,] numbers;

    5、下面是一个完整的 C# 程序,它声明并实例化上面所讨论的数组。

    using System;
    class DeclareArraysSample
    {
        public static void Main()
        {
            // Single-dimensional array
            int[] numbers = new int[5];
    
            // Multidimensional array
            string[,] names = new string[5,4];
    
            // Array-of-arrays (jagged array)
            byte[][] scores = new byte[5][];
    
            // Create the jagged array
            for (int i = 0; i < scores.Length; i++)
            {
                scores[i] = new byte[i+3];
            }
    
            // Print length of each row
            for (int i = 0; i < scores.Length; i++)
            {
                Console.WriteLine("Length of row {0} is {1}", i, scores[i].Length);
            }
        }
    }

    6、初始化数组

    C# 通过将初始值括在大括号 ({}) 内为在声明时初始化数组提供了简单而直接了当的方法。下面的示例展示初始化不同类型的数组的各种方法。

    注意   如果在声明时没有初始化数组,则数组成员将自动初始化为该数组类型的默认初始值。另外,如果将数组声明为某类型的字段,则当实例化该类型时它将被设置为默认值 null。

    一维数组
    int[] numbers = new int[5] {1, 2, 3, 4, 5};
    string[] names = new string[3] {"Matt", "Joanne", "Robert"};
    

    可省略数组的大小,如下所示:

    int[] numbers = new int[] {1, 2, 3, 4, 5};
    string[] names = new string[] {"Matt", "Joanne", "Robert"};
    

    如果提供了初始值设定项,则还可以省略 new运算符,如下所示:

    int[] numbers = {1, 2, 3, 4, 5};
    string[] names = {"Matt", "Joanne", "Robert"};
    
    多维数组
    int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };
    string[,] siblings = new string[2, 2] { {"Mike","Amy"}, {"Mary","Albert"} };
    

    可省略数组的大小,如下所示:

    int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };
    string[,] siblings = new string[,] { {"Mike","Amy"}, {"Mary","Albert"} };
    

    如果提供了初始值设定项,则还可以省略 new 运算符,如下所示:

    int[,] numbers = { {1, 2}, {3, 4}, {5, 6} };
    string[,] siblings = { {"Mike", "Amy"}, {"Mary", "Albert"} };
    
    交错的数组(数组的数组)

    可以像下例所示那样初始化交错的数组:

    int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
    

    可省略第一个数组的大小,如下所示:

    int[][] numbers = new int[][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
    

    -或-

    int[][] numbers = { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
    

    请注意,对于交错数组的元素没有初始化语法。

    7、访问数组成员

    访问数组成员可以直接进行,类似于在 C/C++ 中访问数组成员。例如,下面的代码创建一个名为 numbers 的数组,然后向该数组的第五个元素赋以 5:


    int[] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; numbers[4] = 5;

    下面的代码声明一个多维数组,并向位于 [1, 1] 的成员赋以 5

    int[,] numbers = { {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10} };
    numbers[1, 1] = 5;
    

    下面声明一个一维交错数组,它包含两个元素。第一个元素是两个整数的数组,第二个元素是三个整数的数组:

    int[][] numbers = new int[][] { new int[] {1, 2}, new int[] {3, 4, 5}
    };
    

    下面的语句向第一个数组的第一个元素赋以 58,向第二个数组的第二个元素赋以 667:

    numbers[0][0] = 58;
    numbers[1][1] = 667;

    8、数组是对象

    在 C# 中,数组实际上是对象。System.Array 是所有数组类型的抽象基类型。可以使用System.Array 具有的属性以及其他类成员。这种用法的一个示例是使用“长度”(Length) 属性获取数组的长度。下面的代码将 numbers 数组的长度(为 5)赋给名为 LengthOfNumbers 的变量:

     
     
    int[] numbers = {1, 2, 3, 4, 5};
    int LengthOfNumbers = numbers.Length;
    

    System.Array 类提供许多有用的其他方法/属性,如用于排序、搜索和复制数组的方法。

    对数组使用 foreach

    C# 还提供 foreach 语句。该语句提供一种简单、明了的方法来循环访问数组的元素。例如,下面的代码创建一个名为 numbers 的数组,并用 foreach 语句循环访问该数组:

     
     
    int[] numbers = {4, 5, 6, 1, 2, 3, -2, -1, 0};
    foreach (int i in numbers)
    {
       System.Console.WriteLine(i);
    }
    

    由于有了多维数组,可以使用相同方法来循环访问元素,例如:

     
     
    int[,] numbers = new int[3, 2] {{9, 99}, {3, 33}, {5, 55}};
    foreach(int i in numbers)
    {
       Console.Write("{0} ", i);
    }
    

    该示例的输出为:

     
     
    9 99 3 33 5 55
    

    不过,由于有了多维数组,使用嵌套 for 循环将使您可以更好地控制数组元素。

  • 相关阅读:
    python学习笔记4核心类型字典
    python学习笔记5核心类型元组和文件及其他
    python学习笔记11异常总结
    python学习笔记14类总结
    python学习笔记17常用函数总结整理
    python学习笔记1核心类型数字
    python学习笔记3核心类型列表
    python学习笔记16各种模块和开放工具收集整理
    源码、反码、补码
    素数
  • 原文地址:https://www.cnblogs.com/Shjergsen/p/7069502.html
Copyright © 2011-2022 走看看