二维数组是跨越多个行和列的同质元素的集合,采用矩阵的形式。下面是一个具有 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'注意- 行和列的索引从 0 开始。因此,索引位置 [0,0] 是数组的第一个元素,[m-1, n-1] 是数组的最后一个元素。
阅读所有元素
但是,上面的方法为我们提供了数组中单个元素的值。我们如何遍历整个数组来读取它的每个元素?简单的解决方案是使用嵌套的 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 <p><strong>输出</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172534750857170.png?x-oss-process=image/resize,p_40" class="lazy" alt="C# 中的二维数组" ></p> <p><strong>GetLength() 方法</strong></p> <p>好的。但是,上面的示例仅在我事先知道数组中元素的数量时才有效。如果我的数组是动态的怎么办?如何遍历动态数组的所有元素? GetLength 方法来拯救我们。</p> <p>int arr2D.GetLength(0); //返回第一个维度(行)</p> <p>int arr2D.GetLength(1); //返回第二维(列)</p> <p><strong>代码</strong></p> <pre class="brush:php;toolbar:false">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 <p><strong>输出</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172534751040303.png?x-oss-process=image/resize,p_40" class="lazy" alt="C# 中的二维数组" ></p> <p><strong>每个循环的力量</strong></p> <p>for-each 循环为数组的每个元素执行一组命令。这是一种非常强大的循环机制,强烈建议使用,因为它比传统的 for 循环更高效。</p> <p><strong>代码</strong></p> <pre class="brush:php;toolbar:false">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 <p><strong>Output</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172534751424984.png?x-oss-process=image/resize,p_40" class="lazy" alt="C# 中的二维数组" ></p> <h4>5. Update Elements in C# 2D Array</h4> <p>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.</p> <p><strong>Code</strong></p> <pre class="brush:php;toolbar:false">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 <p><strong>Output</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172534751589570.png?x-oss-process=image/resize,p_40" class="lazy" alt="C# 中的二维数组" ></p> <h4>6. Delete Elements in C# 2D Array</h4> <p>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.</p> <p>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.</p> <p>The solution here is to create a new array without the elements that we want to delete.</p> <p><strong>Code</strong></p> <pre class="brush:php;toolbar:false">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 <p><strong>Output</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172534751646940.png?x-oss-process=image/resize,p_40" class="lazy" alt="C# 中的二维数组" ></p> <h3><strong>Conclusion</strong></h3> <p>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.</p> <h3><strong>Recommended Articles</strong></h3> <p>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-</p> <ol> <li>2D Arrays in Java</li> <li>2D Arrays In Python</li> <li>Arrays in C#</li> <li>Arrays in C++</li> </ol>
以上是C# 中的二维数组的详细内容。更多信息请关注PHP中文网其他相关文章!