首页  >  文章  >  后端开发  >  C# 多维数组

C# 多维数组

WBOY
WBOY原创
2024-09-03 15:12:21591浏览

在C#中,矩形数组或多维数组是指以矩阵格式组织元素。多维数组只能是二维或三维的。数组的维数是指变量中数据的组织格式。因此,我们可以将多维数组定义为按行或列的顺序或顺序排列元素的组织。

语法:

以下是多维数组的语法:

二维数组的声明。

int[,] x=new int[1,2];

3D 数组的声明。

int[,,] x=new int[1,2,3];

上面的语法指定了声明二维和三维数组 (x) 的格式。第一个数组包含两个元素 1 和 2,而三维数组包含元素 1,2,3。

多维数组的初始化

多维数组可以通过三种不同的方式初始化

1。完整声明

int[,] x = new int[6,6];

以上规范完整地初始化了一个二维数组,包括数组类型、数组大小和new运算符的使用。

2。不使用 New 运算符进行初始化

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

3。初始化数组而不声明大小

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

C# 多维数组示例

以下是 C# 中多维数组的示例:

示例#1

用于说明多维数组的声明和初始化的程序。下面的示例说明了在 C# 中创建多维数组。

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
public static void Main(string[] args)
{
int[,] x = { { 3, 2, 1 }, { 6, 5, 4 }, { 9, 8, 7 } };
for (int a = 0; a < 3; a++)
{
for (int b = 0; b < 3; b++)
{
Console.Write(x[a, b] + " ");
}
Console.WriteLine();
}
}
}
}

输出:

C# 多维数组

示例#2

演示二维数组的初始化、声明以及访问元素的程序。

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
/* declaring and initialising a two dimensional array*/
int[,] b = new int[6, 2] { { 1, 2 }, { 4, 3 }, { 5, 6 }, { 8,7 }, { 9 , 10 }, { 2, 3 } };
int i, j;
/* accessing each of the elements value for the array */
for (i = 0; i < 6; i++)
{
for (j = 0; j < 2; j++)
{
Console.WriteLine("a[{0},{1}] = {2}", i, j, b[i, j]);
}
}
Console.ReadKey();
}
}
}

输出:

C# 多维数组

上面的程序演示了如何使用索引作为位置标记来访问多维数组中的数组元素。

示例#3

两个多维数组相加的程序。

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
public static void Main()
{
int[,] array1 = new int[3, 3];
int[,] array2 = new int[3, 3];
int[,] resultArray = new int[3, 3];
int i, j;
Console.WriteLine("specify the members of the first array: ");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
array1[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine("elements of the array1: ");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
Console.Write("{0} ", array1[i, j]);
}
Console.Write("\n");
}
Console.WriteLine("specify the members of the array2: ");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
array2[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine("elements of the array2: ");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
Console.Write("{0} ", array2[i, j]);
}
Console.Write("\n");
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
resultArray[i, j] = array1[i, j] + array2[i, j];
}
}
Console.WriteLine("resultArray of the array1 and array2 looks as below : ");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
Console.Write("{0} ", resultArray[i, j]);
}
Console.Write("\n");
}
}
}
}

输出:

C# 多维数组

使用上面的程序,我们完成了数组的加法操作,将第一个数组中的每个元素添加到第二个数组的计数器元素中。例如array1中第一个元素是1;同样,array2 中的第一个元素是 9。相加的结果应包含一个第一个元素为 10 的数组。

优点和缺点

以下是多维数组的优点和缺点:

优点

  • 多维数组可用于组织数组中的数据子组;多维数组还可以用于在指针数组中存储数据内存地址。
  • 在程序中,多维数组在开始时具有静态大小和初始化。任何大小的扩展都需要在初始化时指定相关的大小。
  • 多维数组可以执行矩阵运算并在相同的变量分配下维护大数据值。
  • 多维数组用于实现栈、堆和队列以及哈希表。

缺点

  • 数组的元素位于连续的内存位置;因此,元素的任何插入和删除都会比单个元素上的类似操作更复杂。
  • 此外,元素不能插入到数组的中间。
  • 如果静态分配分配的内存超过所需的内存,可能会导致浪费和无法释放未使用的内存。这可能会产生负面影响。
  • 与单维数组相比,多维数组可能会更慢,这是一个主要缺点。为了克服这个问题,我们可以用多维数组替换锯齿状数组。

以上是C# 多维数组的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:String Array in C#下一篇:Constructor in C#