2016-03-31 14 views
-1

辞書の配列を持っていて、キーで辞書の値を更新したいのですが、このキーは別の配列にあります。簡単に更新するには?これは私が既に知っているキーで辞書の配列を定義している辞書の配列を更新する

Dictionary<string, int>[] arr_dict = new Dictionary<string,int> [lines.Length]; 
for (int i = 0; i < lines.Length; i++) { 
    arr_dict[i] = new Dictionary<string, int>(init_dict); 
} 

、各キーの初期値は0です。

私の問題は、この値に近づけて更新する方法です。

+0

正確に値を更新したいですか? –

答えて

1
public void Add(Dictionary<string, int> arr_dict, int index, string key) 
{ 
    arr_dict[index][key] = arr_dict[index][key] + 1; 
} 

public void Update(Dictionary<string, int> arr_dict, int index, string key, int value) 
{ 
    arr_dict[index][key] = value; 
} 
+0

これは私が知っている、しかし、私は前に言ったように、私の鍵は100個の異なる鍵の上に別の大きな配列で、私はどのようにarr_dict [キー]に鍵を置くか分からない、それdosent作品 – yosi

+0

私の間違いは、辞書を取り出すための整数。キーを入れることは、キーの値を更新するのと同じコードで行うことができます。 – MrFox

1

あなたは、簡単なアクセスとi番目の辞書ことができます:Dictionary<String,int>ある

arr_dict[i] 

。つまり、Dictionary<TKey,TValue>で定義されているメソッドを呼び出して、データの更新、取得、追加を行うことができます。例えば

arr_dict[0].Add("foo",5); //Add foo -> 5 into the first dictionary 
arr_dict[2].Add("bar",42); //Add bar -> 42 into the third one 

int val = arr_dict[2]["bar"]; //Obtain the value associated with "bar" from the second dictionary 
//etc... 

は、メソッドの網羅とよく文書リストについてmanualを参照してください。

0
private Dictionary<string,int> _dictn = new Dictionary<string,int>(); 
    public void Add_Update_ToDictn(string key, int id) 
    { 
     if (_dictn.ContainsKey(key)) 
     { 
      _dictn[key] = id; 
     } 
     else 
     { 
      _dictn.Add(key, id); 
     }   
    } 

ループ他のソース辞書、この方法のキーと値を渡します。これにより既存の値が更新され、更新されない場合は挿入されます。

for (int i = 0; i < lines.Length; i++) { 
     Add_Update_ToDictn(i,init_dict[i]); 
    } 
関連する問題