如何在多维数组中实现预期的结果? C#

时间:2017-07-03 19:26:04

标签: c# multidimensional-array

我正在尝试使用multidimensional array来使用KeyValuePair来获得输出。

输入:

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[,]获得相同的结果,但我不知道如何在维数组中插入。基本上我试图通过这样的例子来了解multidimensional arrays的工作原理。

2 个答案:

答案 0 :(得分:0)

你不能&#34;插入&#34;在任何数组(单维或多维)中。但是你可以用足够的空间创建数组。

    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#祝你好运!

答案 1 :(得分: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;
    }
}

感谢大家的投入。