开发者

Is there something faster than 3D array?

I ask this question because I have no idea of what class/api to use to achievement what I want.

I have 2 string association, but I need a way that doesnt require to know these 2 association string value to be called via a 3rd value.

I thought of a 3d array first, but I wanted to know if there was something faster and already built to be used in C#. I thought of a Dictionnary at first, but figur开发者_Python百科ed you had to know the key value.

So any idea?

EDIT: Here more details...

I have for example a list of

apple, apple juice

banana, banana juice

orange, citrus juice

lemon, citrus juice

...

Now within another programme there is the fruits and I need to transform them into the appropriate juice. So i need to go throu all the list.


You don't need the key to iterate through a dictionary. However, the order in which you'll recieve the information will most likely not be the same as the one you entered it into if you only use the value.

that being said, can you be most specific about your need. I'm not sure I understand what you want to do.


there is the fruits and I need to transform them into the appropriate juice.

This sounds to me like you have a key (the fruit) and you want to get its corresponding value (the fruit juice). Then all you need is a Dictionary<string, string>.

The below code has been updated since I first posted.

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

The Dictionary<TKey, TValue> class is implemented as a hash table, which has very efficient O(1) key lookups.

Out of curiosity, what was your plan for using a 3D array?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜