Comparing and Sorting Strings String Array

First of all, I would like to say that my knowledge of programming is very simple and learned when you go style. So please bear with me if I speak stupidly.

So, I have a multidimensional string array of which is:

X       Y

4,1 Adelaide
4,2 Interlagos
4,3 Sakhir
4,4 Hungaroring
4,5 Estoril
4,6 Barcelona
4,7 Silverstone
4,8 Mugello
4,9 Hockenheim
4,10 Monte Carlo

      

In the above table, X and Y are 2 dimensions of the array.

Now I have another string array with elements from size X of the above array in an unsorted way. For example,

4,6
5,15
3,7
10,12

      

etc...

Now what I want to do is write code that looks like in array # 2 and assigns the corresponding element from dimension Y of array # 1.

For example, when the code meets 4,6 in array # 2, I want the code to assign the corresponding value, which is Barcelona.

Just a basic snippet or algorithm is what I'm looking for. I'll do it myself.

Thanks in advance!

+3


source to share


4 answers


It looks like table 1 should really be Dictionary<string, string>

displaying "4.6" to "Barcelona". Then you can simply do:

// However you want to populate your data
Dictionary<string, string> mapping = ...;

List<string> values = keys.Select(key => mapping[key]).ToList();

      



Note that this will throw an exception if any of the keys are not mapped - if that's not what you want, please clarify the requirements.

It is not clear how you get this data, or your "multidimensional string array" is string[,]

or string[][]

. If you need to get it as a string array, give us more details and we can explain how to convert it to a dictionary.

+6


source


You have to use a dictionary for this. The dictionary is an internal array. If you pass a key, a pair of values ​​(to insert it), a so-called hash function is applied to the key. This function returns an integer i. The value is stored in array [i]. If you want to get a value from the Dictionary, you only pass the key. The hash function is applied internally, i is calculated and the array [i] is returned. It's too much overhead, but key lookup is slow for large arrays (O (log n) if sorted by keys and O (n) if not sorted at all - if you know the O-notice) where the hash function can be very fast in most applications. Thus, even with large dictionaries, access to the value is fast. (There are a few more tricks in the dictionary that handle the case,when two keys result in the same integer i, but you don't need to worry about that unless you want to implement the dictionary yourself)



Dictionary is also called maps or hash maps in other languages.
+1


source


Not sure if I am interpreting your question correctly ...

Your array # 2, are you saying you want to replace its elements (say "4,6") with "Barcelona"?
If so, then: Loop through array # 2, for each item, use String.split () to get two numeric parts out of it (eg "4" and "6"). Then use Integer.parseInt () to convert them from String to int (call them a, b) and use those int as indices for array # 1, such as array1 [a] [b] to get the Y value.

I assume you really want to use an array because those numbers are small and limited, otherwise use a dictionary as other answers suggest ...

+1


source


If you must get your first dataset as a 2D array, here's how you can turn it into a dictionary:

Dictionary<string, string> dic = new Dictionary<string,string>();

for (int i = 0; i < firstArray.GetLength(0); i++)
{
    dic.Add(firstArray[i, 0], firstArray[i, 1]);
}

      

+1


source







All Articles