C# 二维数组(2d Array)解析

在这篇文章中,我将会用示例讨论二维数组。做为文章的部分内容,我们会讨论:

1、什么是二维数组?

2、以示例解析矩形数组和交错数组。

什么是二维数组?

在C#中,以行和列的形式存储元素的数组称为二维数组。C#中,二维数组也叫多维数组,有两种类型。

1、矩形数组:行和列相等的数组叫做矩形数组。

2、行和列不相等的数组称为交错数组。

C#中的矩形数组

让我们了解一下二维数组的语法,请看一下下面的图表

让我们用示例来更好地理解一下矩形数组

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace _2DarrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] arr = new int[4,5];
            int a = 0;

            foreach (int i in arr)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine("\n");

            for (int i = 0; i < arr.GetLength(0); i++)  //0指的是一维
            {
                for (int j = 0; j < arr.GetLength(1); j++)//1指的是二维
                {
                    a += 5;
                    arr[i, j] = a;
                    Console.WriteLine("i is {0};j is {1}",i,j);
                }
            }
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    Console.Write(arr[i,j] + " ");
                }
            }
            Console.ReadKey();
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace _2DarrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] arr = new int[4,5];
            int a = 0;

            foreach (int i in arr)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine("\n");

            for (int i = 0; i < arr.GetLength(0); i++)  //0指的是一维
            {
                for (int j = 0; j < arr.GetLength(2); j++)//如果将1修改为2,则会报错;
                {
                    a += 5;
                    arr[i, j] = a;
                    Console.WriteLine("i is {0};j is {1}",i,j);
                }
            }
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    Console.Write(arr[i,j] + " ");
                }
            }
            Console.ReadKey();
        }
    }
}

 

 

 系统会提示“索引超出了数组界限”。

在上面 的示例中,我们使用嵌套的for循环来分配二维数组元素。我们也可以在二维数组声明的时候给其赋值。

namespace TwoDimensionalArayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //Assigning the array elements at the time of declaration
            int[,] arr = {{11,12,13,14},
                          {21,22,23,24},
                          {31,32,33,34}};

            //printing values of array using for each loop
            foreach (int i in arr)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine("\n");

            //printing the values of array using nested for loop
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    Console.Write(arr[i, j] + " ");
                }
            }

            Console.ReadKey();
        }
    }
}

 

 

 交错数组

这些也是二维数组,也将以行和列的形式存储数据。但是在这个交错的数组中,每一行的列大小是不同的。那就意味着如果第一行包含5列,第二行可能包含4列,而第三行可能包含10列。所以,你需要记住的一点就是,如果列的大小在每行发生变化,那么它就是一个交错数组。如果所有行中,列的大小保持不变,则它是一个矩形二维数组。

在C#中,交错数组也叫做数组中的数组。这是因为在交错数组的情况下,每一行都是一维数组。因此,在C#中,具有不同列大小的多个一维数组组合形成了一个交错数组。

语法:  <type> [][] <name> = new <type> [rows][];

示例:

int [][] arr = new int[3][];
//Or
int [][] arr = {list of values};

要在C#中声明交错数组,只需在声明时指定数组中所需要的行数即可。

示例如下:

int [][] arr = new int[4][];

在上面的数组声明中,我们指定在数组中需要4行。一旦你指定了你想要的数组的行数,那么你需要如下面所示那样用一维数组初始化每一行的列。

arr[0] = new int[5]; // we want five columns in the first row
arr[1] = new int[6]; // we want six columns in the first row
arr[2] = new int[4]; // we want four columns in the first row
arr[3] = new int[5]; // we want five columns in the first row

交错数组示例如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace TwoDimensionalArayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int[][] arr = new int[4][];

            arr[0] = new int[5];
            arr[1] = new int[6];
            arr[2] = new int[4];
            arr[3] = new int[5];

            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr[i].Length; j++)
                {
                    Console.Write(arr[i][j] + " ");
                }
            }
            Console.WriteLine();

            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr[i].Length; j++)
                {
                    Console.WriteLine("此处j值是{0}",j);
                    arr[i][j] = j++;
                }
            }

            for (int i = 0; i < arr.GetLength(0); i++)
            {
                foreach (int x in arr[i])
                {
                    Console.Write(x + " ");
                }
            }
        }
    }
}

 

在上面的例子中,我们通过使用嵌套的for循环来给交错数组的元素赋值。在交错数组定义的时候,也可以给交错数组赋值。

 

namespace TwoDimensionalArayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // Assigning the values of the jagged array
            // at the time of its declaration
            int[][] arr = {
                            new int[4]{11,12,13,14},
                            new int[5]{21,22,23,24,25},
                            new int[3]{31,32,33}
                           };

            //printing the values of jagged array by using nested for loop
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr[i].Length; j++)
                {
                    Console.Write(arr[i][j] + " ");
                }
            }
            Console.WriteLine();

            //print the values of jagged array by using foreach loop within for loop
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                foreach (int x in arr[i])
                {
                    Console.Write(x + " ");
                }
            }
            Console.ReadKey();
        }
    }
}

 希望这篇文章能帮助到你!

 

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值