2016-08-16 45 views
0

我有2個不同長度的數組列表,我希望檢索數據並按期望的順序排列它們。我的編碼是如下:C#如何從2個不同長度的數組列表中檢索數據?

 ArrayList Alist = new ArrayList(); 
     ArrayList Blist = new ArrayList(); 

     Alist.Add(10); 
     Alist.Add(20); 
     Alist.Add(30); 

     Blist.Add("Y"); 
     Blist.Add("Z"); 
     string Result = string.Empty; 

     for (int i = 0; i < Alist.Count; i++) 
     { 
      Result += Alist[i]; 
      for (int j = 0; j < Blist.Count; j++) 
      { 
       Result += Blist[j]; 
       break; 
      } 
     Console.WriteLine(Result); 
     Console.ReadLine(); 
     } 

我的期望輸出應該是

10Y20Z30

,但這個方案是給輸出

10Y20Y30Y

與此同時,檢測到無法訪問的代碼,這是int j 希望任何人都可以糾正我 謝謝。

+3

你應該使用'名單'。而且,你應該寫一個課。 – SLaks

+0

1.這是什麼目的,爲什麼即使寫這個代碼?家庭作業問題可能? 2.使用通用列表或數組,而不是ArrayList。這會給你類型安全。 – Igor

+0

@Igor是的,這是一個家庭作業,我提交了一個使用數組的版本,我的導師要求我使用數組列表,這使我陷入困境 – user6606188

回答

2
//.... 
var j = 0; 
for (int i = 0; i < Alist.Count; i++) 
{ 
     Result += Alist[i]; 
     if (j < Blist.Count) 
      Result += Blist[j++]; 
    } 
    while (j < Blist.Count) //in case B length> A length 
    Result += Blist[j++]; 

    Console.WriteLine(Result); 
    Console.ReadLine(); 
    //.... 
+0

嗨,這真的很棒,謝謝! – user6606188

0

你可以使用相同的for環路,只是使用了更小的尺寸作爲終止條件,即計算兩個尺寸的min

// define objects 
    ArrayList Alist = new ArrayList(); 
    ArrayList Blist = new ArrayList(); 

    // add elements 
    Alist.Add(10); 
    Alist.Add(20); 
    Alist.Add(30); 

    Blist.Add("Y"); 
    Blist.Add("Z"); 

    // 
    string Result = string.Empty; 

    // accumulate result as: ABABABA with size = 2 * (size of smaller) 
    int min = Math.Min(Alist.Count, Blist.Count); 

    var j = 0; 
    for (int i = 0; i < min; i++) 
    { 
     Result += Alist[i]; 
     Result += Blist[i]; 
     j = i; 
    } 

    // as A always > B then (as OP said), 
    // increment and concatenate the next element of array list A 
    ++j; 
    Result += Alist[j]; 

    // display result 
    Console.WriteLine(Result); 
    Console.ReadLine(); 

輸出:

10Y20Z30

+3

最後的「30」將會丟失。 'j'沒有聲明。 –

+0

@ OlivierJacot-Descombes你是對的,謝謝! – Ziezi

+0

此代碼的輸出是'10Y20Z'而不是'10Y20Z30',請參見[這裏](https://dotnetfiddle.net/mdX70H)。你錯過了「Alist」的第三個元素。 – ekad

0

在你的代碼的問題是,對於Alist元素,您在Blist開始一個新的循環,但之後立刻打破這個循環第一個元素。這解釋了您的結果和「無法訪問的代碼」警告。

要達到您想要的效果,你可以做到以下幾點:

Result = string.Join(string.Empty, Alist.OfType<int>() 
       .Select(
       (i, index) => i.ToString() + (index < Blist.Count ? Blist[index].ToString() : string.Empty))); 

這將所有來自Alist具有相同指數在`Blist'元素的元素。

如果有Blist你可以在末尾添加他們更多的元素:

Result += string.Join(string.Empty, Blist.OfType<string>().Skip(Alist.Count)); 
0

所以,你只需要一個循環,雖然這是一個有點冒險,如果你有不同大小的數組。現在你正在爲每一個Alist價值循環整個Blist,我不認爲這是你想要的。接近問題的非常幼稚的方法是這樣的:

for(int i = 0; i < Alist.Count; i++) { 
    Result+= Alist[i]; 
    Result+= Blist[i]; 
} 

但是你要碰到的問題,一個有3個元素,而B只有2 B [2]會返回一個ArgumentOutOfRangeException。所以讓我們確保你不會遇到這個問題:

for(int i = 0; i < Alist.Count; i++) { 
    Result+= Alist[i]; 
    if(i < Blist.Count) { 
     Result+= Blist[i]; 
    } 
} 

這應該正確地結合列表。如果您不確定哪個列表較大,您可以在循環之前做一些小邏輯,以確保它們能夠正確結合。

ArrayList longerList = (Alist.Count >= Blist.Count) ? Alist : Blist; 
ArrayList shorterList = (Alist.Count >= Blist.Count) ? Blist : Alist; 

for (int i = 0; i < longerList.Count; i++) { 
    Result += longerList[i]; 
    if (i < shorterList.Count) { 
     Result += shorterList[i]; 
    } 
} 
0
 //... 
     StringBuilder strBuilder = new StringBuilder(); 
     var max = Math.Max(Alist.Count, Blist.Count); 
     for (int i = 0; i < max; ++i) 
     { 
      if (Alist.Count > i) 
       strBuilder.Append(Alist[i]); 

      if (Blist.Count > i) 
       strBuilder.Append(Blist[i]); 
     } 

     var result = strBuilder.ToString(); 
     Console.WriteLine(result); 
相關問題