简体   繁体   English

转换KeyValuePair <int,string> 到一个int []数组和string []数组

[英]Convert KeyValuePair<int,string> to an int[] array and string[] array

KeyValuePair<int, string>[][] partitioned = SplitVals(tsRequests.ToArray());

Don't worry too much about the method i use; 不用太担心我使用的方法。 lets just say i get a jagged array of KeyValuePairs that are split equally into different arrays. 可以说我得到了一个交错的KeyValuePair数组,它们被均等地分成不同的数组。

foreach (KeyValuePair<int, string>[] pair in partitioned)
{
    foreach (KeyValuePair<int, string> k in pair)
    {
    }
}

I need to know how i can efficiently get the ints in an int array, and the strings in a seperate string array from the array of keyvaluepairs. 我需要知道如何才能有效地获取int数组中的int以及从键值对数组中分离出的字符串数组中的字符串。 That way both of the indexes match eachother in seperate arrays. 这样,两个索引在单独的数组中彼此匹配。

For example, after i split it into an int[] array and a string[] array, 例如,在将其拆分为int []数组和string []数组后,

intarray[3] must match stringarray[3] just like it did in the keyvaluepair.

Lets say i have a jagged array with KVP like: 可以说我的KVP有一个锯齿状的数组,例如:

    [1][]<1,"dog">, <2,"cat">
    [2][]<3,"mouse">,<4,"horse">,<5,"goat">
    [3][]<6,"cow">

I need this to turn into during each iteration 我需要在每次迭代中将其转化为

    1. 1,2 / "dog","cat"
    2. 3,4,5 / "mouse", "horse", "goat"
    3. 6 / "cow"
int[] keys = partitioned.Select(pairs => pairs.Select(pair => pair.Key).ToArray())
    .ToArray();
string[] values = partitioned.Select(pairs => pairs.Select(pair => pair.Value).ToArray())
    .ToArray();
List<int> keys = new List<int>();
List<string> values = new List<string>();
foreach (KeyValuePair<int, string>[] pair in partitioned)
{
    foreach (KeyValuePair<int, string> k in pair)
    {
        keys.Add(k.Key);
        values.Add(k.Value);
    }
}
keyArray = keys.ToArray();
valueArray = values.ToArray();

Just to get you started: 只是为了让您入门:

    var list = new List<KeyValuePair<int, int>>();
    var key = new int[list.Count];
    var values = new int[list.Count];
    for (int i=0; i < list.Count ;i++)
    {
        key[i] = list[i].Key;
        values[i] = list[i].Value;
    }
public static void ToArrays<K,V>(this IEnumerable<KeyValuePair<K,V>> pairs, 
    out K[] keys, out V[] values)
{
   var keyList = new List<K>();
   var valueList = new List<V>();
   foreach (KeyValuePair<K,V> pair in pairs)
   {
      keyList.Add(pair.Key);
      valueList.Add(pair.Value);
   }
   keys = keyList.ToArray();
   values = valueList.ToArray();
}

Because you have a jagged array, you also need to flatten it out to use that function: 因为您有一个锯齿状的数组,所以还需要将其展平以使用该函数:

public static IEnumerable<T> flatten(this T[][] items)
{
   foreach(T[] nested in items)
   {
      foreach(T item in nested)
      {
         yield return item;
      }
   }
}    

Bring it all together like this: 像这样将所有内容放在一起:

 MyKeyValuePairCollection = GetKeyValuePairJaggedArray();
 int[] keys;
 string[] values;
 MyKeyValuePairCollection.flatten().ToArrays(out keys, out values);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM