因此,假设我有一个helper类,其中包含操作集合的方法:
public static void RemainingDegreeDistribution(IGraph graph, float[,] distArr)
{
int total = 0;
for(int i=0; i < graph.Edges.Count; i++)
{
int startRemDeg = graph.Edges[i].Start.RemDeg;
int endRemDeg = graph.Edges[i].End.RemDeg;
distArr[startRemDeg,endRemDeg]++;
distArr[endRemDeg, startRemDeg]++;
total = total+2;
}
for(int i=0; i < distArr.GetLength(0); i++)
{
for(int j=0; j < distArr.GetLength(1); j++)
{
distArr[i,j] /= total;
}
}
}
如何更改代码以允许传入的集合是数组或我自己的集合类之一?
问题是,传入的集合必须是二维的。没有一个接口可以在我自己的集合类中实现,而数组也可以实现。
我希望能够在这两种情况下重用代码,并避免引入大量丑陋的条件逻辑。我还需要避免分配内存,所以为数组创建某种包装类是不可接受的。我也想避免在列表中使用列表,因为使用普通的二维数组要简单得多,并且列表为我的目的分配了太多的内存。使用索引器或其他工具似乎可以做到这一点。比如,有没有办法声明"distArr“必须是带有两个参数的索引器的任何类型,等等?
发布于 2019-01-02 18:29:30
如果你需要在二维空间中表示点,我建议你使用点数据结构。https://docs.microsoft.com/en-us/dotnet/api/system.drawing.point?view=netframework-4.7.2
如果您还想传递坐标和点的数组,请尝试重载:
public static Method ( float[,] coordinates )
{
//generate array of Points starting from coordinates , points.Add ( new Point(coordinate[i,j]) )
}
public static Method ( Point[] points)
{
//actual logic
}
https://stackoverflow.com/questions/54004138
复制相似问题