2017-07-03 75 views
0

我試圖通過使用multidimensional array來實現輸出,我可以通過使用KeyValuePair來獲得輸出結果。如何在多維數組中實現預期的結果? C#

輸入:

var foodPair = new Dictionary<string, string> 
{ 
    {"Pizza", "Italian"}, 
    {"Curry", "Indian"}, 
    {"Masala", "Indian"} 
}; 

var teamPreference = new Dictionary<string, string> 
{ 
    {"Jose", "Italian" }, 
    {"John", "Indian" }, 
    {"Sarah", "Thai" }, 
    {"Mary", "*" } 
}; 

* means give everything 
If selected food type is not available than give nothing. i.e Thai 

輸出:

Jose, Pizza 
John, Curry 
John, Masala 
Mary, Pizza 
Mary, Curry 
Mary, Masala 

使用KeyValuePair<string, string>工作結果:

https://dotnetfiddle.net/hNdlfy

我想達到相同的結果通過使用string[,]但我不」不知道如何插入角錢nsional數組。基本上我試圖通過這樣的例子來學習multidimensional arrays的工作原理。

+0

沒什麼。當使用字典時,它是一個KeyPairValue,其中的關鍵字是加速查找的哈希。你也可以使用List >。存儲數據時的字典需要比添加到列表<>更長的時間,因爲添加字典必須將密鑰放入散列表中。所以當你不需要散列來檢索數據或者當你想提高寫入速度時,沒有理由不能使用列表而不是字典。 – jdweng

回答

0

您不能「插入」任何數組(單維或多維)。但是你可以創建足夠空間的數組。

var foodPair = new Dictionary<string, string> { { "Pizza", "Italian" }, { "Curry", "Indian" }, { "Masala", "Indian" } }; 
    var teamPreference = new Dictionary<string, string> { { "Jose", "Italian" }, { "John", "Indian" }, { "Sarah", "Thai" }, { "Mary", "*" } }; 
    var results = new List<KeyValuePair<string, string>>(); 

    var results2 = new string[10, 2]; 
    int rowIndex = 0; 

    foreach (var teamMember in teamPreference) 
    { 
     switch (teamMember.Key) 
     { 
      case "Jose": 
       var key = foodPair.FirstOrDefault(x => x.Value == "Italian").Key; 
       results.Add(new KeyValuePair<string, string>(teamMember.Key, key)); 
       results2[rowIndex, 0] = teamMember.Key; 
       results2[rowIndex, 1] = key; 
       rowIndex++; 
       break; 

      case "John": 
       var getAll = foodPair.Where(x => x.Value == "Indian").ToList(); 
       if (getAll.Any()) 
       { 
        results.AddRange(getAll.Select(a => new KeyValuePair<string, string>(teamMember.Key, a.Key))); 
       } 
       foreach (var item in getAll) 
       { 
        results2[rowIndex, 0] = teamMember.Key; 
        results2[rowIndex, 1] = item.Key; 
        rowIndex++; 
       } 
       break; 

      case "Sarah": 
       var c = foodPair.FirstOrDefault(x => x.Value == "Thai").Key; 
       if (!string.IsNullOrEmpty(c)) 
       { 
        results.Add(new KeyValuePair<string, string>(teamMember.Key, c)); 
       } 
       if (!string.IsNullOrEmpty(c)) 
       { 
        results2[rowIndex, 0] = teamMember.Key; 
        results2[rowIndex, 1] = c; 
        rowIndex++; 
       } 

       break; 

      case "Mary": 
       if (teamMember.Value == "*") 
       { 
        var everything = foodPair.Keys.ToList(); 
        if (everything.Any()) 
        { 
         results.AddRange(everything.Select(food => new KeyValuePair<string, string>(teamMember.Key, food))); 
        } 
        foreach (var item in everything) 
        { 
         results2[rowIndex, 0] = teamMember.Key; 
         results2[rowIndex, 1] = item; 
         rowIndex++; 
        } 
       } 

       break; 
     } 
    } 

    if (results.Any()) 
    { 
     foreach (var result in results) 
     { 
      Console.WriteLine("{0}, {1}", result.Key, result.Value); 
     } 
    } 

    Console.WriteLine("Using multidimensional array"); 

    for (int row = 0; row < rowIndex; row++) 
    { 
     Console.WriteLine("{0}, {1}", results2[row, 0], results2[row, 1]); 
    } 

但是多維數組不適合用於此任務的數據結構。 (你可以看到數組看起來很糟糕的代碼)。您需要的數據結構稱爲tuple,結果的定義可能看起來像var results3 = new List<Tuple<string, string>>();
其他注意事項 - 您不需要使用.Any()foreach聲明。對於空集合,不執行foreach
祝你好運,學習C#!

0

我用Extension方法來達到預期的效果。

public static string[,] FoodPreferenceWithDimensionalArray() 
{ 
    var foodPair = new Dictionary<string, string> 
    { 
     {"Pizza", "Italian"}, 
     {"Curry", "Indian"}, 
     {"Masala", "Indian"} 
    }; 

    var teamPreference = new Dictionary<string, string> 
    { 
     {"Jose", "Italian" }, 
     {"John", "Indian" }, 
     {"Sarah", "Thai" }, 
     {"Mary", "*" } 
    }; 

    var results = new List<KeyValuePair<string, string>>(); 
    foreach (var teamMember in teamPreference) 
    { 
     switch (teamMember.Key) 
     { 
      case "Jose": 
       var italianDish = foodPair.FirstOrDefault(x => x.Value == "Italian").Key; 
       results.Add(new KeyValuePair<string, string>(teamMember.Key, italianDish)); 
       break; 

      case "John": 
       var indianDish = foodPair.Where(x => x.Value == "Indian"); 
       if (indianDish.Any()) 
       { 
        results.AddRange(indianDish.Select(dish => new KeyValuePair<string, string>(teamMember.Key, dish.Key))); 
       } 
       break; 

      case "Sarah": 
       var thaiDish = foodPair.FirstOrDefault(x => x.Value == "Thai").Key; 
       if (!string.IsNullOrEmpty(thaiDish)) 
       { 
        results.Add(new KeyValuePair<string, string>(teamMember.Key, thaiDish)); 
       } 
       break; 

      case "Mary": 
       if (teamMember.Value == "*") 
       { 
        var everything = foodPair.Keys.ToList(); 
        if (everything.Any()) 
        { 
         results.AddRange(everything.Select(food => new KeyValuePair<string, string>(teamMember.Key, food))); 
        } 
       } 
       break; 
     } 
    } 

    var resultIn2DArray = results.To2DArray(); 
    return resultIn2DArray; 
} 

// Created extension to convert List to String[,] 
public static class Extension 
{ 
    public static string[,] To2DArray<T>(this List<T> list) 
    { 
     if (list.Count == 0) 
     { 
      throw new ArgumentException("The list must have non-zero dimensions."); 
     } 

     var result = new string[list.Count, list.Count]; 
     for (var i = 0; i < list.Count; i++) 
     { 
      // This is set to 0 since I know the output but will work on this to make it dynamic. 
      result[0, i] = list[i].ToString(); 
     } 

     return result; 
    } 
} 

謝謝大家的意見。