以 LINQ 方式初始化锯齿状数组
我有一个二维锯齿状数组(尽管它始终是矩形),我使用传统循环对其进行初始化:
var myArr = new double[rowCount][];
for (int i = 0; i < rowCount; i++) {
myArr[i] = new double[colCount];
}
我想也许某些 LINQ 函数可以为我提供一种优雅的方法来在一个语句中执行此操作。 然而,我能想到的最接近的是:
double[][] myArr = Enumerable.Repeat(new double[colCount], rowCount).ToArray();
问题是它似乎正在创建一个 double[colCount] 并分配对该引用的引用,而不是为每行分配一个新数组。 有没有办法做到这一点而又不会变得太神秘?
I have a 2-dimensional jagged array (though it's always rectangular), which I initialize using the traditional loop:
var myArr = new double[rowCount][];
for (int i = 0; i < rowCount; i++) {
myArr[i] = new double[colCount];
}
I thought maybe some LINQ function would give me an elegant way to do this in one statement. However, the closest I can come up with is this:
double[][] myArr = Enumerable.Repeat(new double[colCount], rowCount).ToArray();
The problem is that it seems to be creating a single double[colCount]
and assigning references to that intsead of allocating a new array for each row. Is there a way to do this without getting too cryptic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您所拥有的内容将不起作用,因为
new
在调用重复
。 您需要能够重复创建数组的东西。 这可以使用Enumerable.Range 来实现
方法 生成范围,然后执行选择
操作,将范围的每个元素映射到新的数组实例(如艾米 B 的回答)。但是,我认为您正在尝试使用 LINQ,但在这种情况下这样做并不合适。 在使用 LINQ 解决方案之前所拥有的一切都很好。 当然,如果您想要类似于
Enumerable 的 LINQ 风格方法。重复
,你可以编写自己的扩展方法来生成一个新的项目,例如:然后你可以按如下方式调用它:
What you have won't work as the
new
occurs before the call toRepeat
. You need something that also repeats the creation of the array. This can be achieved using theEnumerable.Range
method to generate a range and then performing aSelect
operation that maps each element of the range to a new array instance (as in Amy B's answer).However, I think that you are trying to use LINQ where it isn't really appropriate to do so in this case. What you had prior to the LINQ solution is just fine. Of course, if you wanted a LINQ-style approach similar to
Enumerable.Repeat
, you could write your own extension method that generates a new item, such as:Then you can call it as follows:
行为是正确的 -
Repeat()
返回一个多次包含所提供对象的序列。 您可以执行以下技巧。The behavior is correct -
Repeat()
returns a sequence that contains the supplied object multiple times. You can do the following trick.您不能使用
Repeat
方法来做到这一点:element
参数仅计算一次,因此它实际上总是重复相同的实例。 相反,您可以创建一个方法来执行您想要的操作,该方法将采用 lambda 而不是值:You can't do that with the
Repeat
method : theelement
parameter is only evaluated once, so indeed it always repeats the same instance. Instead, you could create a method to do what you want, which would take a lambda instead of a value :我刚刚写了这个函数......
似乎有效。
用法:
I just wrote this function...
Seems to work.
Usage:
怎么样
或
参考:http://msdn.microsoft .com/en-us/library/aa691346(v=vs.71).aspx
What about
or
Reference: http://msdn.microsoft.com/en-us/library/aa691346(v=vs.71).aspx