我们怎么解决这种类型?

时间:2013-01-13 21:25:45

标签: c# c#-4.0 c#-3.0 c#-2.0

for (int i = 2; i < k; i++)
{
    if ((tabl1[i].y != null) && (tabl[i].x != null))
    {
        double[] y2 = { 0, tabl1[i].y };
        double[] x2 = { tabl[i].x, 0 };
        PointPairList spl3 = new PointPairList(x2, y2);
    }
}

我想在i=3然后创建

double[] y3 = { 0, tabl1[i].y }; 
double[] x3 = { tabl[i].x, 0 }; 
PointPairList spl3 = new PointPairList(x3, y3);

i=4然后创建

double[] y4 = { 0, tabl1[i].y };
double[] x4 = { tabl[i].x, 0 }; 
PointPairList spl4 = new PointPairList(x4, y4);

i=5然后创建

double[] y5 = { 0, tabl1[i].y };
double[] x5 = { tabl[i].x, 0 };
PointPairList spl5 = new PointPairList(x5, y5);

等。

3 个答案:

答案 0 :(得分:1)

问题绝对不是克莱尔......像这样?

private static Dictionary<String, PointPairList> s_PointPairLists = new Dictionary<String, PointPairList>();

private static void BuildPointPairLists(Int32 limit)
{
    for (Int32 i = 2; i < limit; ++i)
    {
        if ((tabl[i].x != null) && (tabl[i].y != null))
        {
            Double[] x = { 0, tabl[i].y };
            Double[] y = { tabl[i].x, 0 };

            s_PointPairLists[("ppl" + i.ToString())] = new PointPairList(x, y);
        }
    }
}

public static PointPairList(Int32 index)
{
    String reference = "ppl" + index.ToString();

    if (s_PointPairs.Contains(reference))
        return s_PointPairs[reference];

    return null;
}

答案 1 :(得分:0)

将其变成一个函数并调用:

public PointPairList GetPointPairList(int index) {
    if (tabl[index].y != null && tabl[index].x != null) {
        double[] x = { 0, tabl[index].y };
        double[] y = { tabl[index].x, 0 };
        return new PointPairList(x, y);
    }
    return null;
}

// ...
for (int i = 2; i < k; i++) {
    PointPairList ppl = GetPointPairList(i);
    // ...
}

这不符合您的要求吗?没有更多的背景,很难说..

答案 2 :(得分:0)

您应该考虑使用Dictionary

var dict = new Dictionary<int, PointPairList>();
for (int i = 2; i < k; i++)
{
    if ((tabl1[i].y != null) && (tabl[i].x != null))
    {
        double[] y2 = { 0, tabl1[i].y };
        double[] x2 = { tabl[i].x, 0 };
        dict.Add(i, new PointPairList(x2, y2));
    }
}

有了这个,你可以很容易地得到例如以这种方式提出的第四项:dict[4]