gpt4 book ai didi

c# - LINQ 将数组 [x0,y0, ..., xN, yN] 转换为可枚举的 [p0, ..., pN]

转载 作者:太空狗 更新时间:2023-10-29 22:24:08 26 4
gpt4 key购买 nike

是否可以使用 LINQ 转换包含坐标元组 (x, y) 的平面 double 组,即[x0,y0, ..., xN, yN] 到长度为一半的数组,其中包含包装在 Point 类中的相同坐标,即 [p0, ..., pN]?

最好是 .NET 3.5,但也对 4.0 感兴趣。

最佳答案

您可以使用 .Batch来自 Jon Skeet 的 morelinq :

IEnumerable<Point> points = coordinates.Batch(2,pair => new Point(pair.ElementAt(0), pair.ElementAt(1)));

老实说,最简单的解决方案可能是使用一种方法(这里使用 ints):

public IEnumerable<Point> GetPoints(int[] coordinates)
{
for (int i = 0; i < coordinates.Length; i += 2)
{
yield return new Point(coordinates[i], coordinates[i + 1]);
}
}

关于c# - LINQ 将数组 [x0,y0, ..., xN, yN] 转换为可枚举的 [p0, ..., pN],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6592604/

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