C#中的字典.帮助要求 [英] Dictionary in C# . help require

查看:59
本文介绍了C#中的字典.帮助要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象
Dictionary<int,> temp = new Dictionary<int,>();
temp中的值是:

I have an object
Dictionary<int,> temp = new Dictionary<int,>();
The values in temp are:

1 s1
  s2
  s3
2 s1
  s2

我想将其转换为对象
Dictionary<string,int[]> temp = new Dictionary< String,int[]>();
所以我想要最终的o/p为

I want to transform it into object
Dictionary<string,int[]> temp = new Dictionary< String,int[]>();
So i want final o/p as

s1 1
   2
s2 1
   2
s3 1




[更新]
(代表OP)
@CPillani ..您的代码工作很酷,谢谢
如果输入的o/p类型更改如下,请更新ur code ..




[Update]
(On behalf of the OP)
@CPillani.. ur code working cool thanks
If the input,o/p type changed as follows.. please update ur code ..

i/p = Dictionary<uri,> d1 = new Dictionary<uri,>();
o/p = Dictionary<string[],> socialTermUriNew = new Dictionary<string[],>();


[/Update]


[/Update]

推荐答案

如果确实需要整数数组,则必须在Dictionary上迭代两次.
例如(rd输出 Dictionary)

If you really need an array of integers the you have to iterate two times on the Dictionary.
e.g. (rd is the output Dictionary)

static void Main(string[] args)
    {
      string s1 = "s1";
      string s2 = "s2";
      string s3 = "s3";
      string[] a1 = { s1, s2, s3 };
      string[] a2 = { s1, s2 };
      

      Dictionary <int, string[]> d = new Dictionary<int, string[]>();
      d.Add(1, a1);
      d.Add(2, a2);
      

      Dictionary <string,int > cd = new Dictionary<string, int>();
      Dictionary <string, int> cdi = new Dictionary<string, int>();
      
      foreach (int k in d.Keys)
      {
        foreach (string s in d[k]) 
        {
          if (cd.ContainsKey(s))
            cd[s]++;
          else
            cd.Add(s, 1);
        }  
      }
      Dictionary<string, int[]> rd = new Dictionary<string, int[]>();


      foreach (string s in cd.Keys)
      {
        rd.Add(s, new int[cd[s]]);
        cdi.Add(s, 0);
      }

      

      foreach (int k in d.Keys)
      {
        foreach (string s in d[k])
        {
          rd[s][cdi[s]++] = k;
        }
      }
    }



[更新]
我没有完全满足您的新要求(我看不到您的泛型类型的正确声明),但是我想您可以修改我提供的代码来满足您的需求(这应该不难).
[/Update]



[Update]
I don''t get your new requirements completely (I cannot see proper declaration of your generic types), but I suppose you may modify the code I provided to fit your needs (it shouldn''t be hard).
[/Update]


我认为您应该使用属性类

I think you should use property classes

public class UserDefinedDictionary{
public string strText =new string();
public int[5] intArr=new int[5];
}

Dictionary<userdefineddictionary> udDictionary=new Dictionary<userdefineddictionary>();

udDictionary.strText="s1";
udDictionary.intArr[0]=1;
udDictionary.intArr[1]=2;

udDictionary.strText="s2"
udDictionary.intArr[0]=1;
udDictionary.intArr[1]=2;

udDictionary.strText="s3";
udDictionary.intArr[0]=1;


希望对您有帮助.

让我知道是否存在语法问题.

最好的问候;
纳兹什·阿巴西(Nazish Abbasi)


hope this might help you.

Do let me know if there is some syntax issue.

Best Regards;
Nazish Abbasi


这篇关于C#中的字典.帮助要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆