有比3D阵列更快的东西吗?

时间:2011-02-11 19:30:30

标签: c# arrays

我问这个问题,因为我不知道用什么类/ api来实现我想要的东西。

我有2个字符串关联,但我需要一种不需要知道通过第3个值调用这2个关联字符串值的方法。

我首先想到了一个3d数组,但我想知道是否有更快的东西并且已经构建好用于C#。我起初想到了一个Dictionnary,但想到你必须知道关键值。

所以任何想法?

编辑:这里有更多细节......

我有一个

列表 苹果,苹果汁 香蕉,香蕉汁
橙子,柑橘汁
柠檬,柑橘汁 ...

现在在另一个程序中有水果,我需要将它们转化为适当的果汁。所以我需要通过所有列表。

2 个答案:

答案 0 :(得分:4)

您不需要密钥来遍历字典。但是,如果您仅使用该值,则您收到信息的顺序很可能与您输入的顺序不同。

话虽如此,您是否可以最具体地了解您的需求。我不确定我明白你想做什么。

答案 1 :(得分:3)

  

我需要的是水果   将它们转化为适当的   汁。

这听起来像你有一个(水果),你想得到相应的(果汁)。然后你需要的只是一个Dictionary<string, string>

以下代码自我首次发布以来已更新。

// OK, so we'll say this comes from an external program. I am just constructing
// it here for illustration.
var fruitJuices = new Dictionary<string, string>
{
    { "Apple", "Apple juice" },
    { "Banana", "Banana juice" }
    /* etc. */
};

// This list comes from the user.
List<string> fruits = GetFruitsFromUser();

foreach (string fruit in fruits)
{
    string matchingFruitJuice;
    if (fruitJuices.TryGetValue(fruit, out matchingFruitJuice))
    {
        // Do whatever you need with this juice.
        CreateFlavor(matchingFruitJuice);
    }
    else
    {
        // Either report on the non-existence of this flavor of juice,
        // or possibly just do nothing.
    }
}

Dictionary<TKey, TValue>类实现为hash table,具有非常高效的O(1)密钥查找。

出于好奇,您使用3D阵列的计划是什么?