2D阵列。将所有值设置为特定值
问题描述:
我要使用LINQ为一维数组分配特定值,
To assign specific value to 1D array I'm using LINQ like so:
int[] nums = new int[20];
nums = (from i in nums select 1).ToArray<int>();
nums[0] = 2;
在2D([x,y])数组中有类似的方法吗?
还是较短的方法,而不使用嵌套循环?
There is similar way to do so in 2D ([x,y]) array? Or short way, without using nested loops?
答
LINQ在多维数组上的效果不佳
LINQ doesn't work particularly well with multi-dimensional arrays.
锯齿状数组还不错:
var array = Enumerable.Range(0, 10)
.Select(x => Enumerable.Repeat('x', 10).ToArray())
.ToArray();
...但是矩形数组没有任何特定的支持。只需使用循环即可。
... but rectangular arrays don't have any specific support. Just use loops.
(请注意,使用 Enumerable.Repeat
作为创建1-的较为简单的方法维度数组,顺便说一句。)
(Note the use of Enumerable.Repeat
as a somewhat simpler approach to creating the 1-dimensional array, btw.)