1

I've hunted high and low on the net and not been able to find a working answer for this.

I need to create a variable that is a 3 dimensional array but the first reference point should be string and the second double.

e.g. readings["alpha"][] = 17.42; readings["alpha"][] = 19.42; readings["beta"][] = 11.1;

Please note I cant reference the index's as I don't know how many index's there will be!

I would then use a for each on the array to get the values, so a for each on the alpha would return

17.42, 19.42

Any one have any ideas as I'm dumbfounded as to why I cant just create an array and use strings as the index instead of int values.

5 Answers 5

2

That's because arrays in C# are actual arrays (flat lists in contiguous memory), unlike the multi-purpose key-value collections PHP calls 'arrays'. Because of this, using anything but zero-based integers for indexes doesn't make much sense.

If you want a collection that supports string keys, look around the System.Collections and System.Collections.Generic namespaces. You probably want something like a Dictionary<String, double[][]>.

Sign up to request clarification or add additional context in comments.

1 Comment

How would I add to this dictonary value? I've tried readings[reading][] = value, readings.Add(reading, value) but nothing seems to work
1

Not an array but why dont do the following...

    internal Dictionary<string, List<double>> _readings = new Dictionary<string, List<double>>();

    public void Test ()
    {
        _readings.Add("alpha", new List<double>() { 17.42, 19.42 });

        // getting alpha values
        var alphaValues = from p in _readings where p.Key == "alpha" select p.Value;
        foreach (double d in alphaValues.First())
            Console.WriteLine(d);
    }

if you want to have a function for adding one double (as you sad in you comment)

    private void  AddRegarding(string key, double value)
    {
        if (_readings[key] == null)
            _readings[key] = new List<double>();
        _readings[key].Add(value);
    }

5 Comments

The 3d array is being stored in an object and a function called to add the reading from a spectrometer, am I correct in saying I could just do _readings.Add("alpha", value);
no cause the value type of the dictionary is "List<double>" but you can create a function that does that for you.
don't quite get what you mean?
i changed my answer. It now has the additional function you want.
Thanks dknaack, I have to modify your response slightly to get it to work within the class I setup but I can report ITS WORKING!!! I've made changes to your post and just added the class at the bottom for anyone who has similar issues!
1

Why don't you use a Hash Table for your outer two data structures that references a list.

Some languages may even allow you to use a (String, Double) pair as a key, if you don't need access to the second layer.

Edit: Since it was made clear that this is C#, you should look into the Dictionary data structure.

Comments

1

You could use a

Dictionary<string, double[]>

or just have a lookup dictionary for the string index

Dictionary<string, int> lookup = new Dictionary<string, int> { {"alpha", 0}, {"beta", 1} };
double[][] readings;
// do some init obviously, then use it
readings[lookup["alpha"]][0] = 1;

2 Comments

so I would use an alias type of system? I'll give it a try!
Object reference not set to an instance of an object. int offset = readings[lookup[reading]].Length; readings[lookup[reading]][offset] = value;
0

You mean an associative array, not just a simple array. A simple array is just blocks of memory positioned one after the other and the access to them is by saying how far are they from the beginning, so they cant be "three" away they can only be 3 away.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.