我有一个二维数组,我想将其清除并重置为 0 值。我知道如何使用 Array.Clear()
清除向量(一维数组),但我不知道清除二维矩阵的最佳方法。
double D = new double[10];
Array.Clear(D, 0, D.Length);
如何清除一个 2D N x M 数组
double D[,] = new double[N,M];
感谢您提供的任何帮助。
最佳答案
Array.Clear
也适用于多维数组:
double[,] D = new double[M,N];
Array.Clear(D, 0, D.Length);
注意,不需要自己计算长度,因为 Length
属性返回元素的总数,而不管维数:
A 32-bit integer that represents the total number of elements in all the dimensions of the Array; zero if there are no elements in the array.
关于c# - 在 C# 中将二维数组归零的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19669482/