gpt4 book ai didi

c# - 在 C# 中将二维数组归零的最快方法

转载 作者:可可西里 更新时间:2023-11-01 03:13:38 25 4
gpt4 key购买 nike

我有一个二维数组,我想将其清除并重置为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com