转换现有的代码片段以使用Array.ForEach

时间:2022-07-18 22:04:41

We know that if you have:

我们知道,如果你有:

var aa = new [] { 1, 2, 3, 4, 5 };
for (int i = 0; i < aa.length; ++i)
{
    aa[i] = aa[i] + 1;
}

it's really

var aa = new [] { 1, 2, 3, 4, 5 };
Arrary.ForEach(aa, a => a + 1);

However, what if I had this:

但是,如果我有这个:

var aa = new [] { 1, 2, 3, 4, 5 };
var ab = new [] { 1, 2, 3, 4, 5 };
for (int i = 0; i < aa.length; ++i)
{
    aa[i] = ab[i] + 1;
}

Can I convert this to use just one Array.ForEach? Or, how would you do it, if you wanna go all functional programming crazy? Clunky for loops just looks ugly.

我可以将其转换为仅使用一个Array.ForEach吗?或者,如果你想让所有功能编程疯狂,你会怎么做?笨拙的循环看起来很难看。

6 个解决方案

#1


How about:

aa = ab.ConvertAll(x => x+1);

#2


It's not pretty, but this will work (using the Select() overload that returns the index of the element):

它不漂亮,但这将工作(使用返回元素索引的Select()重载):

var aa = new[] { 1, 2, 3, 4, 5 };
var ab = new[] { 1, 2, 3, 4, 5 };

Array.ForEach(
    aa.Select((x, i) => i).ToArray(),
    i => aa[i] = ab[i] + 1);

IMHO a simple "for" loops is a lot easier to understand in this case.

在这种情况下,恕我直言,一个简单的“for”循环更容易理解。

#3


Another similar approach:

另一种类似方法:

Enumerable.Range(0, ab.Length).Select(i => aa[i] = ab[i] + 1);
// Will not work if aa.Length != ab.Length

#4


This won't be fast, but it just might work

这不会很快,但它可能会起作用

var aa = new [] { 1, 2, 3, 4, 5 };
var ab = new [] { 1, 2, 3, 4, 5 };
Array.ForEach(aa, new Action(a => ab[Array.IndexOf(a)] + 1));

Unfortunately thiswill not work for an array with duplicate elements.

不幸的是,这对于具有重复元素的数组不起作用。

#5


var aa = new[] { 1, 2, 3, 4, 5 };
var ab = new[] { 1, 2, 3, 4, 5 };

var index = 0;
Array.ForEach(aa, a => { aa[index] = ab[index] + 1; index++; });

Would this not work? Its 1 Array.ForEach with the indexer held outside of the Array.ForEach.

这不行吗?它的1个Array.ForEach,索引器保存在Array.ForEach之外。

#6


Not a direct answer, but you want ForEach2, which iterates over two sequences/arrays. Unfortunately this is not included with the BCL, so you'll have to add it.

不是直接的答案,但你想要ForEach2,它迭代两个序列/数组。不幸的是,这不包括在BCL中,因此您必须添加它。

But if you're trying to do functional in C#, you'll probably have a helper library anyways, so it's not that big of a deal.

但是如果你想在C#中做功能,你可能会有一个帮助库,所以这不是什么大不了的事。

#1


How about:

aa = ab.ConvertAll(x => x+1);

#2


It's not pretty, but this will work (using the Select() overload that returns the index of the element):

它不漂亮,但这将工作(使用返回元素索引的Select()重载):

var aa = new[] { 1, 2, 3, 4, 5 };
var ab = new[] { 1, 2, 3, 4, 5 };

Array.ForEach(
    aa.Select((x, i) => i).ToArray(),
    i => aa[i] = ab[i] + 1);

IMHO a simple "for" loops is a lot easier to understand in this case.

在这种情况下,恕我直言,一个简单的“for”循环更容易理解。

#3


Another similar approach:

另一种类似方法:

Enumerable.Range(0, ab.Length).Select(i => aa[i] = ab[i] + 1);
// Will not work if aa.Length != ab.Length

#4


This won't be fast, but it just might work

这不会很快,但它可能会起作用

var aa = new [] { 1, 2, 3, 4, 5 };
var ab = new [] { 1, 2, 3, 4, 5 };
Array.ForEach(aa, new Action(a => ab[Array.IndexOf(a)] + 1));

Unfortunately thiswill not work for an array with duplicate elements.

不幸的是,这对于具有重复元素的数组不起作用。

#5


var aa = new[] { 1, 2, 3, 4, 5 };
var ab = new[] { 1, 2, 3, 4, 5 };

var index = 0;
Array.ForEach(aa, a => { aa[index] = ab[index] + 1; index++; });

Would this not work? Its 1 Array.ForEach with the indexer held outside of the Array.ForEach.

这不行吗?它的1个Array.ForEach,索引器保存在Array.ForEach之外。

#6


Not a direct answer, but you want ForEach2, which iterates over two sequences/arrays. Unfortunately this is not included with the BCL, so you'll have to add it.

不是直接的答案,但你想要ForEach2,它迭代两个序列/数组。不幸的是,这不包括在BCL中,因此您必须添加它。

But if you're trying to do functional in C#, you'll probably have a helper library anyways, so it's not that big of a deal.

但是如果你想在C#中做功能,你可能会有一个帮助库,所以这不是什么大不了的事。