Suppose I have this multidimensional array:
假设我有这个多维数组
float[][,] vertices = {
new float[,]{ {0f, 1.28f}, {1.28f, 2.56f}, {3.84f, 2.56f}, {5.12f, 1.28f}, {3.84f, 0f}, {1.28f, 0f}, {0f, 1.28f} },
new float[,]{ {0f, 3.83f}, {1.27f, 5.12f}, {3.87f, 5.12f}, {5.12f, 3.83f}, {5.12f, 1.26f}, {3.87f, 0f}, {1.27f, 0f}, {0f, 1.26f}, {0f, 3.83f} }
};
Now, I want to convert each subarray to an array of type Vector2[]
where Vector2
is a public class, which simply contains x
and y
properties:
现在,我想要将每个子数组转换为一个类型为Vector2的数组[],其中Vector2是一个公共类,它只包含x和y属性:
public class Vector2 {
public float x;
public float y;
public Vector2(float x, float y) { this.x = x; this.y = y }
}
So I want to construct Vector2 elements from Array[2] elements, which are subarrays in above vertices
array variable.
我想从数组[2]元素中构造向量2元素,它们是顶点数组变量上面的子数组。
I do it like this:
我是这样做的:
Array.ConvertAll(vertices[0],
new Converter<float[], Vector2>(verticesSequence => { return new Vector2(verticesSequence[0], verticesSequence[1]); }));
However, in return I receive this error message:
但是,作为回报,我收到了这个错误消息:
Error 15 The best overloaded method match for 'System.Array.ConvertAll(float[][], System.Converter)' has some invalid arguments
错误15最佳重载方法匹配'System.Array。ConvertAll(float[][], System.Converter)'有一些无效的参数
2 个解决方案
#1
2
You have an array, which contains two arrays, each of which contains a different number of float arrays.
您有一个数组,它包含两个数组,每个数组包含不同数量的浮动数组。
Array.ConvertAll
is suitable for converting one array into the other by specifying a mapping delegate (one to one). In this case, you don't only have to convert a single float[,]
into a Vector2
. Note that you also used a float[]
to Vector2
converter, instead of float[,]
to Vector2
.
数组中。ConvertAll适用于通过指定映射委托(一对一)将一个数组转换为另一个数组。在这种情况下,您不仅需要将单个浮点[,]转换为Vector2。注意,您还使用了一个float[]到Vector2转换器,而不是float[,]到Vector2。
Multidimensional arrays like float[,]
are also a bit tricky since they don't support LINQ out of the box, which it a bit harder to create a one-liner which would do all the mapping.
像float[,]这样的多维数组也有点棘手,因为它们不支持开箱即用的LINQ,创建一个能够完成所有映射的一行程序有点困难。
In other words, you will at least need a helper method to map the items of the multidimensional array:
换句话说,您至少需要一个辅助方法来映射多维数组的项:
public static IEnumerable<Vector2> ConvertVectors(float[,] list)
{
for (int row = 0; row < list.GetLength(0); row++)
{
yield return new Vector2(list[row, 0], list[row, 1]);
}
}
And then you can use that inside the Array.ConvertAll
method:
然后你可以在数组中使用它。ConvertAll方法:
var converted = Array.ConvertAll<float[,], Vector2[]>(
vertices,
ff => ConvertVectors(ff).ToArray());
Honestly, I would prefer a LINQ solution because it will infer all the generic parameters automatically:
老实说,我更喜欢LINQ解决方案,因为它会自动推断出所有的泛型参数:
var r = vertices
.Select(v => ConvertVectors(v).ToArray())
.ToArray();
#2
2
As mentioned in comments the errors you are getting is because you were trying to pass in a multidimensional array instead of a jagged array.
正如在注释中提到的,您所得到的错误是因为您试图传递一个多维数组而不是一个交错数组。
For your needs it may be easier to just use a simple loop
对于您的需要,使用一个简单的循环可能更容易
List<Vector2> newList = new List<Vector2>();
foreach (float[,] array in vertices)
for (int i = 0; i < array.GetLength(0); i++ )
newList.Add(new Vector2(array[i,0], array[i,1]));
Note: this loops through all vertices so the outer loop may not be required for your needs.
注意:这个循环遍历所有的顶点,因此外部循环可能不需要您的需要。
#1
2
You have an array, which contains two arrays, each of which contains a different number of float arrays.
您有一个数组,它包含两个数组,每个数组包含不同数量的浮动数组。
Array.ConvertAll
is suitable for converting one array into the other by specifying a mapping delegate (one to one). In this case, you don't only have to convert a single float[,]
into a Vector2
. Note that you also used a float[]
to Vector2
converter, instead of float[,]
to Vector2
.
数组中。ConvertAll适用于通过指定映射委托(一对一)将一个数组转换为另一个数组。在这种情况下,您不仅需要将单个浮点[,]转换为Vector2。注意,您还使用了一个float[]到Vector2转换器,而不是float[,]到Vector2。
Multidimensional arrays like float[,]
are also a bit tricky since they don't support LINQ out of the box, which it a bit harder to create a one-liner which would do all the mapping.
像float[,]这样的多维数组也有点棘手,因为它们不支持开箱即用的LINQ,创建一个能够完成所有映射的一行程序有点困难。
In other words, you will at least need a helper method to map the items of the multidimensional array:
换句话说,您至少需要一个辅助方法来映射多维数组的项:
public static IEnumerable<Vector2> ConvertVectors(float[,] list)
{
for (int row = 0; row < list.GetLength(0); row++)
{
yield return new Vector2(list[row, 0], list[row, 1]);
}
}
And then you can use that inside the Array.ConvertAll
method:
然后你可以在数组中使用它。ConvertAll方法:
var converted = Array.ConvertAll<float[,], Vector2[]>(
vertices,
ff => ConvertVectors(ff).ToArray());
Honestly, I would prefer a LINQ solution because it will infer all the generic parameters automatically:
老实说,我更喜欢LINQ解决方案,因为它会自动推断出所有的泛型参数:
var r = vertices
.Select(v => ConvertVectors(v).ToArray())
.ToArray();
#2
2
As mentioned in comments the errors you are getting is because you were trying to pass in a multidimensional array instead of a jagged array.
正如在注释中提到的,您所得到的错误是因为您试图传递一个多维数组而不是一个交错数组。
For your needs it may be easier to just use a simple loop
对于您的需要,使用一个简单的循环可能更容易
List<Vector2> newList = new List<Vector2>();
foreach (float[,] array in vertices)
for (int i = 0; i < array.GetLength(0); i++ )
newList.Add(new Vector2(array[i,0], array[i,1]));
Note: this loops through all vertices so the outer loop may not be required for your needs.
注意:这个循环遍历所有的顶点,因此外部循环可能不需要您的需要。