二维数组是跨越多个行和列的同质元素的集合,采用矩阵的形式。下面是一个具有 m 行和 n 列的 2D 数组的示例,从而创建一个 m x n 配置的矩阵。
[ a1, a2, a3, a4, ..., an b1, b2, b3, b4, ..., bn c1, c2, c3, c4, ..., cn . . . m1, m2, m3, m4, ..., mn ]
锯齿数组是数组的数组。交错数组本质上是多个数组交错在一起形成多维数组。二维锯齿状数组可能看起来像这样:
[ [ a1, a2, a3, a4, ..., an ], [ b1, b2, b3, b4, ..., b20 ], [ c1, c2, c3, c4, ..., c30 ], . . . [ m1, m2, m3, m4, ..., m25 ] ]
请注意,锯齿状数组的所有行可能包含也可能不包含相同数量的元素。
从实现的角度来看,锯齿数组与真正的二维数组完全不同。了解 C# 如何实现多维数组和锯齿数组非常重要。
编程语言在多维数组的实现上有所不同。一些编程语言(如 C、C++、C#、Fortran 等)支持真正的二维数组。虽然还有其他人使用数组数组(也称为锯齿状数组)来模拟这种行为。那么,真正的二维数组与锯齿状数组有何不同?
多维数组的两种实现在存储消耗方面是不同的。虽然真正的 2D 数组每行有 m 行 n 元素,但锯齿状数组可能有 m 行,每行具有不同数量的元素。这使得数据集的浪费空间最小化。因此,下面的锯齿状数组完全没问题:
int[][] jagged array = [ [1, 2, 3, 4], [5, 6, 7], [8, 9] ]
如果相同的数据集要在真正的二维数组中实现,则会如下所示:
int[,] multiDimArray = [ 1, 2, 3, 4 5, 6, 7, 0 8, 9, 0, 0 ]
这里,对二维数组的一些操作如下:
让我们看看如何在 C# 中声明 2D 数组,以及如何在 C# 中不声明 2D 数组。
怎么做?
C# 中真正的二维数组实现从数组声明开始。如下所示:
int[,] arr2D; string[,] arr2D_s;
定义中逗号的数量决定了数组的维数。请注意,您不能在数组声明中指定数组的大小。它必须在数组初始化期间完成。
如何不呢?
二维数组和锯齿状数组的实现很容易混淆。锯齿状数组声明如下所示:
int[][] jagged array;
下一步是初始化我们刚刚声明的二维数组。有多种方法可以做到这一点。
使用新运算符
arr2D = new int[2,3]; //separate initialization string[,] arr2D_s = new string[4,5]; //with declaration
使用值初始化
//without dimensions arr2D = new int[,]{{1,2}, {3,4}, {5,6}}; //with declaration arr2D_s = new string[2,2]{{"one","two"},{"three", "four"}};
没有新干员
Int[,] arr2D_a = {{1,2}, {3,4}, {5,6}, {7,8}};
读取单个元素
下一个操作是从二维数组中读取元素。由于二维数组是 m x n 元素的矩阵,因此每个元素都有指定的行索引和列索引组合。我们可以通过在下标中提供行索引和列索引来访问元素。示例如下:
int[,] arr2D_i = {{1,2}, {3,4}, {5,6}, {7,8}}; string arr2D_s = {{"one","two"},{"three", "four"}}; int val_i = arr2D_i[2,1]; //returns '6' string val_s = arr2D_s[1,1]; //returns 'four'
阅读所有元素
但是,上面的方法为我们提供了数组中单个元素的值。我们如何遍历整个数组来读取它的每个元素?简单的解决方案是使用嵌套的 for/while 循环遍历整个数组。
代码
using System; public class Program { public static void Main() { int[,] arr2D_i = new int[3, 3]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; //reading all the elements through for loop for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { Console.Write(arr2D_i[i, j] + "\t"); } Console.WriteLine("\n"); } } }
输出
GetLength() 方法
好的。但是,上面的示例仅在我事先知道数组中元素的数量时才有效。如果我的数组是动态的怎么办?如何遍历动态数组的所有元素? GetLength 方法来拯救我们。
int arr2D.GetLength(0); //返回第一个维度(行)
int arr2D.GetLength(1); //返回第二维(列)
代码
using System; public class Program { public static void Main() { int[,] arr2D_i = new int[3, 3]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; //reading all the elements through for loop for (int i = 0; i < arr2D_i.GetLength(0); i++) { for (int j = 0; j < arr2D_i.GetLength(1); j++) { Console.Write(arr2D_i[i, j] + "\t"); } Console.WriteLine("\n"); } } }
输出
每个循环的力量
for-each 循环为数组的每个元素执行一组命令。这是一种非常强大的循环机制,强烈建议使用,因为它比传统的 for 循环更高效。
代码
using System; public class Program { public static void Main() { string[,] arr2D_s = new string[3, 3]{{"one", "two", "three"}, {"four","five","six"}, {"seven","eight","nine"}}; //reading all the elements through foreach loop foreach(var ele in arr2D_s) { Console.WriteLine(ele); } } }
输出
现在让我们看一个有关如何在 C# 二维数组中插入元素的示例。思路是遍历数组的每个位置并为其赋值。
代码
using System; public class Program { public static void Main() { int[,] arr2D_i = new int[3, 3]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int[,] squares = new int[3, 3]; int[,] cubes = new int[3, 3]; for (int i = 0; i < arr2D_i.GetLength(0); i++) { for (int j = 0; j < arr2D_i.GetLength(1); j++) { squares[i, j] = arr2D_i[i, j] * arr2D_i[i, j]; cubes[i, j] = squares[i, j] * arr2D_i[i, j]; } } Console.WriteLine("Squares\n"); DisplayArray(squares); Console.WriteLine("\n\nCubes\n"); DisplayArray(cubes); } static void DisplayArray(int[, ] arr) { for (int i = 0; i < arr.GetLength(0); i++) { for (int j = 0; j < arr.GetLength(1); j++) { Console.Write(arr[i, j] + "\t"); } Console.WriteLine("\n"); } } }
Output
We will update our array to multiply each element with 2. The idea is to traverse each position of the array and update the value it holds.
Code
using System; public class Program { public static void Main() { int[, ] arr2D_i = new int[3, 3]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Console.WriteLine("Original Array\n"); DisplayArray(arr2D_i); for (int i = 0; i < arr2D_i.GetLength(0); i++) { for (int j = 0; j < arr2D_i.GetLength(1); j++) { arr2D_i[i, j] *= 2; } } Console.WriteLine("\n\nUpdated Array (multiplied by 2)\n"); DisplayArray(arr2D_i); } static void DisplayArray(int[, ] arr) { for (int i = 0; i < arr.GetLength(0); i++) { for (int j = 0; j < arr.GetLength(1); j++) { Console.Write(arr[i, j] + "\t"); } Console.WriteLine("\n"); } } }
Output
This is a tricky operation. It is not possible to delete a single element from a true C# 2D Array. Deleting a single element will disturb the dimensions of the array such that it would no longer be a matrix. C# does not allow that unless it is a jagged array.
So, what is the solution? Do we delete the entire row or the entire column? No, C# would not allow that as well. The array is fixed in size when declared or initialized. It has fix bytes of memory allocated. We cannot change that at run time.
The solution here is to create a new array without the elements that we want to delete.
Code
using System; public class Program { public static void Main() { int[,] arr2D_i = new int[3, 3]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int[,] new_array = new int[2,2]; Console.WriteLine("Original Array\n"); DisplayArray(arr2D_i); int rowToDel = 2; int colToDel = 2; for (int i = 0; i < arr2D_i.GetLength(0); i++) { if(i==rowToDel) continue; for (int j = 0; j < arr2D_i.GetLength(1); j++) { if(j==colToDel) continue; new_array[i,j]=arr2D_i[i,j]; } } Console.WriteLine("\n\nArray after deleting elements\n"); DisplayArray(new_array); } static void DisplayArray(int[, ] arr) { for (int i = 0; i < arr.GetLength(0); i++) { for (int j = 0; j < arr.GetLength(1); j++) { Console.Write(arr[i, j] + "\t"); } Console.WriteLine("\n"); } } }
Output
Thus, we have seen how a 2D Array is implemented in C# and what are the various CRUD operations we can perform on it. We also learned the difference between a true 2D implementation and a jagged array. There are a lot more methods available in C# to assist the developers with working with Arrays at ease. Do check them out at the MSDN docs.
This is a guide to 2D Arrays in C#. Here we discuss the concept of jagged arrays along with operations on 2D arrays in C#. You may also look at the following articles to learn more-
以上是C# 中的二维数组的详细内容。更多信息请关注PHP中文网其他相关文章!