C#中的数组

发布时间 2024-01-11 16:20:50作者: LilyFlower

C#中的数组

数组的基本定义

数组实际上是由一个变量名表示同一组类型的额数据元素。关于数组,这里有一些重要的定义:

  • 元素:数组的独立数据项称之为元素,数组中的所有元素必须是相同的类型;
  • 维度/秩:数组的维度数;
  • 维度长度:数组方向的位置个数;
  • 数组长度:数组的所有维度中的元素总数称之为数组的长度;
  • C#中的数组一旦被创建,打小就固定了,C#不支持动态数组,且默认的索引号为0。

对于数组来讲,数组就是对象,数组是引用类型。

一维数组

一维数组可以认为是单行或者单列向量。

对于一维数组的定义:

namespace CSharpProject1;

class Program
{
    static void Main(string[] args)
    {
        int[] arr = new int[10]; 
    }
}

访问一位数组中的元素

Console.WriteLine(arr[0]);

结果如下:

D:/RiderProjects/CSharpProject1/CSharpProject1/bin/Debug/net8.0/CSharpProject1.exe
0

Process finished with exit code 0.

在一维数组数组中有特定的性能优化指令,而在矩形数组和交错数组中并没有性能优化指令。

矩形数组

二维数组的定义和取值:

namespace CSharpProject1;

class Program
{
    static void Main(string[] args)
    {
        int[,] arr1 = new int[5, 7];    //5行7列的二维数组
        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 7; j++) arr1[i, j] = i * j;
        }
        Console.WriteLine(arr1[4,5]);
    }
}

结果如下:

D:/RiderProjects/CSharpProject1/CSharpProject1/bin/Debug/net8.0/CSharpProject1.exe
20

Process finished with exit code 0.

三维数组的定义及其使用

namespace CSharpProject1;

class Program
{
    static void Main(string[] args)
    {
        int[,,] arr2 = new int[5, 7, 9];    //三维数组,维数分别为5/7/9
        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 7; j++)
            {
                for (int k = 0; k < 9; k++) arr2[i, j, k] = i * j * k;
            }
        }
        Console.WriteLine(arr2[3, 4, 5]);
    }
}

结果如下所示:

D:/RiderProjects/CSharpProject1/CSharpProject1/bin/Debug/net8.0/CSharpProject1.exe
60

Process finished with exit code 0.

交错数组

交错数组是数组的数组,也就是说在数组里面再定义数组。需要注意的是,交错数组里面的子数组的元素个数可以不同,如下就是声明一个交错数组及其用法:

namespace CSharpProject1;

class Program
{
    static void Main(string[] args)
    {
        int[][] arr3 = new int[3][];                    //实例化顶层数组
        arr3[0] = new int[5] { 1, 2, 3, 4, 5 };         //实例化子数组
        arr3[1] = new int[6] { 1, 2, 3, 4, 5, 6 };      //实例化子数组
        arr3[2] = new int[7] { 1, 2, 3, 4, 5, 6, 7 };   //实例化子数组
        Console.WriteLine(arr3[1][2]);          		//第二个子数组中第三个元素
    }
}

结果如下:

D:/RiderProjects/CSharpProject1/CSharpProject1/bin/Debug/net8.0/CSharpProject1.exe
3

Process finished with exit code 0.

C#中数组常用的方法

namespace CSharpProject1;

class Program
{
    static void Main(string[] args)
    {
        int[] arr = new int[]{1, 5, 4, 3, 65, 7, 8, 9, 10};
        //获取数组的维度数
        Console.WriteLine($"数组的维度数为:{arr.Rank}");
        //获取数组的长度
        Console.WriteLine($"数组的长度为:{arr.Length}");
        //排序
        Array.Sort(arr);
        Console.WriteLine("排序后的元素为");   //1 3 4 5 7 8 9 10 65 
        foreach(int e in arr) Console.Write($"{e} ");
        Console.WriteLine();
        //二分查找
        int index = Array.BinarySearch(arr,5);
        Console.WriteLine($"元素5在数组中的索引位置为{index}");
        Console.WriteLine();
        //将数组中的元素反转
        Array.Reverse(arr);
        foreach(int e in arr) Console.Write($"{e} ");
        Console.WriteLine();
        //返回数组中的索引
        int u = Array.IndexOf(arr,5);
        Console.WriteLine($"索引为5对应的元素为:{u}");
        //克隆数组
        int[] cloneArr = (int[])arr.Clone();
        Console.WriteLine("克隆后的数组为:");
        foreach(int e in cloneArr) Console.Write($"{e} ");
        Console.WriteLine();
        //将数组中的元素清空
        Array.Clear(arr);
    }
}

运行结果如下:

D:/RiderProjects/CSharpProject1/CSharpProject1/bin/Debug/net8.0/CSharpProject1.exe
数组的维度数为:1
数组的长度为:9            
排序后的元素为            
1 3 4 5 7 8 9 10 65       
元素5在数组中的索引位置为3
                          
65 10 9 8 7 5 4 3 1       
索引为5对应的元素为:5     
克隆后的数组为:           
65 10 9 8 7 5 4 3 1       

Process finished with exit code 0.

注意,克隆方法为数组进行浅复制,也就是说,它只创建了数组本身的额克隆,会产生两个独立的数组;如果克隆引用类型数据的数组则会产生指向相同对象的两个数组。