Sunday, 15 September 2013

Dictionary iterator in c# -



Dictionary iterator in c# -

i want know if there way store value of enumerator of key value pairs while iterating through dictionary. want store key , value of enumerator in variable. solution? wanted while iterating through dictionary have reference of current key value pair , next keyvalue pair in dictionary.i dont know why not working

here solution might like:

using system; using system.collections.generic; using system.linq; using system.text; using system.collections; using system.diagnostics; namespace wellpuzzle { class solution { hashtable h1 = new hashtable(); list<int> listofitemstoremove = new list<int>(); dictionary<int, int> d1 = new dictionary<int, int>(); public void falling_disks(int[] a, int[] b) { var itemstoremove = new list<int>(); var en = d1.getenumerator(); int count = 0; (int = 0; <= a.length - 1; i++) { d1.add(count++, a[i]); } //for each incoming element in array foreach (int ele in b) { //store prev current position of enumerator var prev = new keyvaluepair<int, int>(); prev = en.current; //check if possible iterate next element in dictionary if (en.movenext()) { //loop till end of dictionary while (en.movenext()) { //if current value of enumerator in dictionary less incoming element , check if corroesponding key value in hashtable or not if (en.current.value <= ele && !(checkifthatvalueisfilled(en.current.key))) continue; else {//if current enumerator value greater incoming element array b remove elements prev reference till end of dictionary h1.add(en.current.key, true); listofitemstoremove.add(en.current.key); } prev = en.current; } if (!(h1.containskey(en.current.key))) { h1.add(en.current.key, true); listofitemstoremove.add(en.current.key); } } else { h1.add(prev.key, true); listofitemstoremove.add(prev.key); } foreach (int item in listofitemstoremove) { (int = item; < d1.count; i++) { d1.remove(i++); } } } console.writeline(h1.count); } public bool checkifthatvalueisfilled(int value) { if (h1.containsvalue(h1.containskey(value)) == true) homecoming true; else homecoming false; } } class programme { static void main(string[] args) { int[] = new int[] { 5, 6, 4, 3, 6, 2, 3 }; int[] b = new int[] { 2, 3 }; solution s1 = new solution(); s1.falling_disks(a, b); } } }

is there reason cannot use:-

// replace tkey , tvalue types dictionary tkey previouskey; tvalue previousvalue; bool first = true; foreach(var key in dictionary.keys) { var value = dictionary[key]; if (!first) { ... // whatever need keys , values } previouskey = key; previousvalue = value; first = false; }

(note, though, you'll have .orderby(...) .keys create sense)

c# dictionary

No comments:

Post a Comment