Dictionary<string, int> dic = new Dictionary<string, int>();dic.add("香蕉",1);我现在知道如何通过"香蕉"来获取1 --->dic[香蕉]根据1,如何取得"香蕉"?

解决方案 »

  1.   


                foreach (string s in dic.Keys)
                    if (dic[s] == 1)
                        Console.WriteLine(s);
      

  2.   

     foreach (string key in dic.Keys)
                {
                    if (dic[key] == 1)
                        MessageBox.Show(key); //  值为1的key可能有多个.
                }
      

  3.   

    楼上搞错了Dictionary <TKeys, TValues>"香蕉"是键
      

  4.   

    建议lz有空好好看看数据结构方面的书,看看Dictionary到底用来做什么。
      

  5.   

    好像不行吧,
    dic.add("香蕉",1); 
    "香蕉"是KEY吧,
    你也可以添加两个1,比如:
    dic.add("香蕉",1); 
    dic.add("苹果",1); 
    如果你根据1来取,结果是"香蕉"还是"苹果"?
      

  6.   

    foreach(object obj in 集合的名字.values) 

    foreach(object obj1 int 集合的名字.keys) 

    if(集合[obj1]==obj) 

    得到了 


    }
      

  7.   


    static void Main(string[] args)
    {
        string[] strName = { "中国", "武汉", "北京", "南宁", "武汉", "广州", "北京", "西藏", "武汉" };    Dictionary<string, int> dic = new Dictionary<string, int>();
        foreach (string s in strName)
        {
            if (dic.ContainsKey(s))
                dic[s]++;
            else
                dic[s] = 1;
        }    //在此继续写代码,返回字符串中出现最多字符(此注释上面不允许写)}
      

  8.   


            static void Main(string[] args)
            {
                string[] strName = { "中国", "武汉", "北京", "南宁", "武汉", "广州", "北京", "西藏", "武汉" };            Dictionary<string, int> dic = new Dictionary<string, int>();
                foreach (string s in strName)
                {
                    if (dic.ContainsKey(s))
                        dic[s]++;
                    else
                        dic[s] = 1;
                }            int max = 0;
                string name = "";
                foreach (string s in dic.Keys)
                    if (dic[s] > max)
                    {
                        max = dic[s];
                        name = s;
                    }
                Console.WriteLine("{0}出现次数最多,出现了{1}次", name, max);
                
            }