2013-01-13 64 views
-4
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

..是for循環真的需要嗎?當你已經知道你想要的索引時,你正在使用循環。 –

+0

你爲什麼用[tag:facebook-c#-sdk]標記這個?這真的很重要嗎? – Oded

+0

然後你想怎麼處理那些spl1,spl2,spl3,spl4等? – SergeyS

回答

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; 
} 
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); 
    // ... 
} 

難道不是做什麼,你需要?如果沒有更多的背景下,很難說..

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]

相關問題