foreach (KeyValuePair<string, string> a in data)
{
}
因为涉及到修改,用FOR行不行?怎么用呢

解决方案 »

  1.   

    for (int i = 0; i < data.GetLength(); i++)
    {
        KeyValuePair<string, string> a = data[i];
    }
      

  2.   

    en for (int i = 0; i < data.GetLength(); i++)
    {
        KeyValuePair<string, string> a = data[i];
    }
      

  3.   

    楼上两位,看清是: dictionary,不支持索引
      

  4.   

    你既然明知道不支持索引,为什么还问能不能用for?再说为什么涉及到修改就一定要用for呢? 能不能说明一下?如果一定要用for的话,是不是说明用dictionay是不合适的呢?
      

  5.   

    不知道像这样把键值对转化出来满足你的要求不?
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main()
            {
                //初始化dictionary对象
                Dictionary<string, string> dict1 = new Dictionary<string, string>();
                dict1.Add("LeftValue", "1");            List<DictValue> listDict = new List<DictValue>();
                            Dictionary<string, string>.KeyCollection kc = dict1.Keys;
                Dictionary<string, string>.ValueCollection vc = dict1.Values;
                foreach(string k1 in kc)
                    foreach (string v1 in vc)
                    {
                        DictValue dv = new DictValue();
                        dv.key = k1;
                        dv.value = v1;
                        listDict.Add(dv);
                    }
                // 接下来,使用映射对象 listDict进行遍历            for (int i = 0; i < listDict.Count; i++)
                {
                    Console.WriteLine("key:{0},value:{1}", listDict[i].key, listDict[i].value);
                }            Console.ReadLine();
            }
        }    class DictValue
        {
            public string key="";
            public string value="";
        }}
      

  6.   

    dictionary是key value对。
    如果只是想修改value的话
    可以使用
                Dictionary<string, string> a = new Dictionary<string,string>();
                foreach( string key in a.Keys)
                {
                    a[key] = ...
                }
    如果想删除 使用Remove.
      

  7.   

    在FOREACH里不能删除,但可以记录下想要删除的KEY,然后保存到数组当中,FOR遍历数组,按KEY挨个删除DICTIONARY,我是这样做的。
      

  8.   

    同情楼主,回答的全是一群猪,还自为为是高手,哎,连基本常识都没有 要解决在字典中遍历就必须要弄成数组,没有办法的,先把keys取出来做一个数组,再用数组去遍历
      

  9.   

    连数星 带数勋章的人 都乱建议看来真的是无知,不知道星是怎么得来的,想当然,到底试过没有自己
    哎,技术界呀的人,再说一一猪头们,这样做到底行不行你息试一下再说不要想当然
     
    Dictionary<string, string> a = new Dictionary<string,string>(); foreach( string key in a.Keys) { a[key] = ... } 
      

  10.   

    看看我经过测试的无上正解 Dictionary<int, string> dict = new Dictionary<int, string>();
            dict.Add(1, "helo");
            dict.Add(2, "helo");
            int[] keys = new int[dict.Count];
            
            dict.Keys.CopyTo(keys, 0);
            foreach (int key in keys)
            {
                Response.Write("key"+key.ToString()+":" + key.ToString());
                dict.Remove(key);
             //   Response.Write("key" + key.ToString() + ":" + dict[key]);
            }