lsy
December 16th, 2007, 11:14 PM
i declare an ArrayList obj... is there anyway can provide me add an element not in sequencial mode?? example i insert element index 1 before index 0?
|
Click to See Complete Forum and Search --> : ArrayList lsy December 16th, 2007, 11:14 PM i declare an ArrayList obj... is there anyway can provide me add an element not in sequencial mode?? example i insert element index 1 before index 0? ashukasama December 16th, 2007, 11:42 PM use System.Collections.Generic.LinkedList lsy December 17th, 2007, 01:04 AM use System.Collections.Generic.LinkedList It also can't allowed me insert in any index... i have to know the current index before i can insert... Any other array which allow me insert to any index? Arjay December 17th, 2007, 01:56 AM Both the generic List class and the non-generic ArrayList class has an Insert method which takes an index to specify where to insert a new item. In general, prefer to use the generic collections over the older non-generic counterparts (because the generic ones are typesafe). lsy December 17th, 2007, 01:58 AM Both the generic List class and the non-generic ArrayList class has an Insert method which takes an index to specify where to insert a new item. In general, prefer to use the generic collections over the older non-generic counterparts (because the generic ones are typesafe). I'm using ArrayList but when i try to insert at index 1 before index 0, an exception occur... anyway to omit this? Homogenn December 17th, 2007, 03:40 AM I'm using ArrayList but when i try to insert at index 1 before index 0, an exception occur... anyway to omit this? It can't be done with an arraylist. You have two options. If you want to use the ArrayList or List you have to insert it at index 0 and then when another comes, insert that at index 0, so the first one becomes index 1 (I assume this isn't what you're looking for, though) The other is to use a dictionary as such: string a = "A"; string b = "B"; Dictionary<int, string> dict = new Dictionary<int, string>(); // if you want "A" to have value 1 and "B" to have value 0 dict.Add(1, a); dict.Add(0, b); // Find "A" again Console.WriteLine(dict[1]); lsy December 17th, 2007, 04:06 AM It can't be done with an arraylist. You have two options. If you want to use the ArrayList or List you have to insert it at index 0 and then when another comes, insert that at index 0, so the first one becomes index 1 (I assume this isn't what you're looking for, though) The other is to use a dictionary as such: string a = "A"; string b = "B"; Dictionary<int, string> dict = new Dictionary<int, string>(); // if you want "A" to have value 1 and "B" to have value 0 dict.Add(1, a); dict.Add(0, b); // Find "A" again Console.WriteLine(dict[1]); if i add in the key not in sequencial way... how can i use loop to read back all the key value?? ashukasama December 17th, 2007, 04:42 AM if i add in the key not in sequencial way... how can i use loop to read back all the key value?? Not clear lsy December 17th, 2007, 05:09 AM Not clear dict.Add(1, "hyj"); dict.Add(6, "de"); dict.Add(10,"asdf"); if use the count it will return 3 so how can i know which key is with value? Homogenn December 17th, 2007, 08:30 AM dict.Add(1, "hyj"); dict.Add(6, "de"); dict.Add(10,"asdf"); if use the count it will return 3 so how can i know which key is with value? Problem is that the dictionary can contain the same value with several keys, just like an ArrayList can contain the same value with several indexes. You'll have to 'hackfix' it for this reason: // Print out values foreach (string s in dict.Values) Console.WriteLine(s); // Print out values with keys: foreach (string s in dict.Values) { Console.Write("Value: " + s + " Keys: "); foreach (int key in dict.Keys) if (dict[key] == s) Console.Write(key + " "); Console.Write("\n"); } Though, why'd you want to? ;o lsy December 17th, 2007, 09:54 PM Problem is that the dictionary can contain the same value with several keys, just like an ArrayList can contain the same value with several indexes. You'll have to 'hackfix' it for this reason: // Print out values foreach (string s in dict.Values) Console.WriteLine(s); // Print out values with keys: foreach (string s in dict.Values) { Console.Write("Value: " + s + " Keys: "); foreach (int key in dict.Keys) if (dict[key] == s) Console.Write(key + " "); Console.Write("\n"); } Though, why'd you want to? ;o is it possible the dictionary contains 1 key and 2 values and how the value get read? Homogenn December 18th, 2007, 03:05 AM is it possible the dictionary contains 1 key and 2 values and how the value get read? No. One key can only point to one value, like one index can only point to one value. Otherwise it can't find one of them. Arjay December 18th, 2007, 10:42 AM Lsy, if you give us a general, high level description of what you are trying to accomplish, we may be able to suggest an approach. Thread1 December 18th, 2007, 10:35 PM ^ I second the motion :D lsy December 18th, 2007, 10:41 PM Lsy, if you give us a general, high level description of what you are trying to accomplish, we may be able to suggest an approach. i'm looking for something like an array or structure with 1 index hold multiple value! so the index will be my reference point for that particular record or values... Kevin McFarlane December 21st, 2007, 06:27 AM i'm looking for something like an array or structure with 1 index hold multiple value! so the index will be my reference point for that particular record or values... Sounds like you need a multi-dictionary. The free PowerCollections library on codeplex provides such a data structure. PowerCollections (http://www.codeplex.com/PowerCollections) MultiDictionary (http://www.wintellect.com/PowerCollections/Docs/Wintellect.PowerCollections.MultiDictionary%602.html) Here's a usage example: MultiDictionary Example (http://www.codekeep.net/snippets/2310d2c2-f729-45de-a21c-17d531a4c91a.aspx) Homogenn December 21st, 2007, 10:49 AM i'm looking for something like an array or structure with 1 index hold multiple value! so the index will be my reference point for that particular record or values... So, a multidimensional array? ;o aniskhan December 21st, 2007, 12:54 PM Sample using hashtableprivate void Form1_Load_1(object sender, System.EventArgs e) { Hashtable h = new Hashtable(); h.Add(1, new Student("anis", "khan")); h.Add(10, new Student("sami", "khan")); Text = ((Student)h[10]).FirstName; }private struct Student { private string m_FirstName; private string m_LastName; public Student(string firstName, string lastName) { m_FirstName = firstName; m_LastName = lastName; } public object FirstName { get { return m_FirstName; } } public object LastName { get { return m_LastName; } } } codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |