I would like to use something like this:
Dictionary<int, string>[] matrix = new Dictionary<int, string>[2];
But, when I do:
matrix[0].Add(0, "first str");
It throws " 'TargetInvocationException '...Exception has been thrown by the target of an invocation."
What is the problem? Am I using that array of dictionaries correctly?
Dictionaries are Python's implementation of a data structure that is more generally known as an associative array. A dictionary consists of a collection of key-value pairs. Each key-value pair maps the key to its associated value.
An array is an object that contains collections of other objects. Array objects in Objective-C are handled using the Foundation Framework NSArray class. The NSArray class contains a number of methods specifically designed to ease the creation and manipulation of arrays within Objective-C programs.
If you are going to get elements by positions (index) in the array then array will be quicker (or at least not slower than dictionary). If you are going to search for elements in the array than dictionary will be faster.
Try this:
Dictionary<int, string>[] matrix = new Dictionary<int, string>[] { new Dictionary<int, string>(), new Dictionary<int, string>() };
You need to instantiate the dictionaries inside the array before you can use them.
Did you set the array objects to instances of Dictionary?
Dictionary<int, string>[] matrix = new Dictionary<int, string>[2]; matrix[0] = new Dictionary<int, string>(); matrix[1] = new Dictionary<int, string>(); matrix[0].Add(0, "first str");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With