Multi-dimensional arraylist or list in C#? -
It is possible to create a multi-dimensional list in C #. I can create such a multi-dimensional array:
string [,] result = new string [20, 2];
But I want to be able to use certain features in a list or array list such as being able to add and delete elements.
You can create lists of lists
Public class multidimensionalists: List & lt; List & lt; String & gt; & Gt; {}
or a dictionary of key-accessible lists
Public class multidimensional digit list: dictionary & lt; String, list & lt; Int & gt; & Gt; {} Multidimetic catalog myDicList = new multidimensional (); MyDicList.Add ("age", new listing & lt; int & gt; ()); MyDicList.Add ("Pay", new listing & lt; int & gt; ()); MyDicList.Add ("Account ID", new listing & lt; int & gt; ());
To apply the suggestion in comment from the generic version, @ user 420667,
public class multidimensionalists & lt; T & gt; List & lt; List & lt; T & gt; & Gt; {}
For more dictionaries,
public class multidimidict list & lt; K, T & gt;: Dictionary & lt; K, list & lt; T & gt; & Gt; {} // To use it, var myDicList = new multidimensional in client code & lt; String, int & gt; (); MyDicList.Add ("age", new list & lt; T & gt; ()); . MyDicList ["age"] pairs (23); . MyDicList ["age"] pairs (32); . MyDicList ["age"] pairs (18); MyDicList.Add ("Pay", New List & lt; T & gt; ()); . MyDicList ["Pay"] Add (80000); . MyDicList ["Salary"] Add (100000); MyDicList.Add ("Account ID", new listing & lt; T & gt; ()); . Add MyDicList ["accountIds"] (321,123); . Add MyDicList ["accountIds"] (342653);
Or, even better, ...
public class multidimensional dictlists & lt; K, T & gt;: Dictionary & lt; K, list & lt; T & gt; & Gt; {Add public zeros (k, t aobject) {if (add key (key) in it (key, new list & lt; T & gt; ()); If (! Base [key] is included (add object)) base [key]. Add-ons; }} // and to use it, var myDicList = new Multidimensional Dictlete in client code & lt; String, int & gt; (); MyDicList.Add ("age", 23); MyDicList.Add ("age", 32); MyDicList.Add ("age", 18); MyDicList.Add ("Salary", 80000); MyDicList.Add ("Salary", 110000); MyDicList.Add ("Accountwise", 321123); MyDicList.Add ("Account ID", 342653); Edit: To include an add method for nested example: Public class NestedDelidiedDistrict list K, K2, T & gt;: Multimedia List & l; K, Multidimensional Dictylist & Lt; K2, T & G; & Gt;: Add the public key (Key Key, Key Key 2, T add Object) {If (! Contains the key!) (Key, New Multidimix List & lt; K2, T & gt; ()) ; If (! Base [Key] is included in (key2) Base [Key]. Add (key2, addObject); }}
Comments
Post a Comment