Multi-dimensional arraylist or list in C#?

157,190

Solution 1

You can create a list of lists

   public class MultiDimList: List<List<string>> {  }

or a Dictionary of key-accessible Lists

   public class MultiDimDictList: Dictionary<string, List<int>>  { }
   MultiDimDictList myDicList = new MultiDimDictList ();
   myDicList.Add("ages", new List<int>()); 
   myDicList.Add("Salaries", new List<int>()); 
   myDicList.Add("AccountIds", new List<int>()); 

Generic versions, to implement suggestion in comment from @user420667

  public class MultiDimList<T>: List<List<T>> {  }

and for the dictionary,

   public class MultiDimDictList<K, T>: Dictionary<K, List<T>>  { }

  // to use it, in client code
   var myDicList = new MultiDimDictList<string, int> ();
   myDicList.Add("ages", new List<T>()); 
   myDicList["ages"].Add(23);
   myDicList["ages"].Add(32);
   myDicList["ages"].Add(18);

   myDicList.Add("salaries", new List<T>());
   myDicList["salaries"].Add(80000);
   myDicList["salaries"].Add(100000);

   myDicList.Add("accountIds", new List<T>()); 
   myDicList["accountIds"].Add(321123);
   myDicList["accountIds"].Add(342653);

or, even better, ...

   public class MultiDimDictList<K, T>: Dictionary<K, List<T>>  
   {
       public void Add(K key, T addObject)
       {
           if(!ContainsKey(key)) Add(key, new List<T>());
           if (!base[key].Contains(addObject)) base[key].Add(addObject);
       }           
   }


  // and to use it, in client code
    var myDicList = new MultiDimDictList<string, int> ();
    myDicList.Add("ages", 23);
    myDicList.Add("ages", 32);
    myDicList.Add("ages", 18);
    myDicList.Add("salaries", 80000);
    myDicList.Add("salaries", 110000);
    myDicList.Add("accountIds", 321123);
    myDicList.Add("accountIds", 342653);

EDIT: to include an Add() method for nested instance:

public class NestedMultiDimDictList<K, K2, T>: 
           MultiDimDictList<K, MultiDimDictList<K2, T>>: 
{
       public void Add(K key, K2 key2, T addObject)
       {
           if(!ContainsKey(key)) Add(key, 
                  new MultiDimDictList<K2, T>());
           if (!base[key].Contains(key2)) 
               base[key].Add(key2, addObject);
       }    
}

Solution 2

you just make a list of lists like so:

List<List<string>> results = new List<List<string>>();

and then it's just a matter of using the functionality you want

results.Add(new List<string>()); //adds a new list to your list of lists
results[0].Add("this is a string"); //adds a string to the first list
results[0][0]; //gets the first string in your first list

Solution 3

Not exactly. But you can create a list of lists:

var ll = new List<List<int>>();
for(int i = 0; i < 10; ++i) {
    var l = new List<int>();
    ll.Add(l);
}

Solution 4

Depending on your exact requirements, you may do best with a jagged array of sorts with:

List<string>[] results = new { new List<string>(), new List<string>() };

Or you may do well with a list of lists or some other such construct.

Solution 5

If you want to modify this I'd go with either of the following:

List<string[]> results;

-- or --

List<List<string>> results;

depending on your needs...

Share:
157,190
etoisarobot
Author by

etoisarobot

Updated on January 05, 2020

Comments

  • etoisarobot
    etoisarobot over 4 years

    Is it possible to create a multidimensional list in C#? I can create an multidimensional array like so:

     string[,] results = new string[20, 2];
    

    But I would like to be able to use some of the features in a list or arraylist like being able to add and delete elements.

  • user420667
    user420667 over 12 years
    Yup. Might want to make this generic by having MultiList<baseT> : List<List<baseT>>.
  • Matheno
    Matheno almost 11 years
    This post saved my weekend :D
  • Mike Lowery
    Mike Lowery about 10 years
    In the last example, I think you want to enclose both Adds within the if statement block.
  • Charles Bretana
    Charles Bretana about 10 years
    @DiskCrasher, Almost, I think the second Add needs to be in its own conditional, but good catch - thanks!
  • htm11h
    htm11h over 8 years
    This is not multi-dimensional, it is two dimensional.
  • Valkyrie
    Valkyrie about 7 years
    This is wonderful! Any idea how I'd construct the Add() statement when I'm nesting two of these suckers together? md.Add(int1, Add(int2, string)); ?
  • Charles Bretana
    Charles Bretana about 7 years
    Added a modified class to represent nested instance with add() Method
  • Valkyrie
    Valkyrie about 7 years
    Thank you so much, Charles!
  • user3772108
    user3772108 over 6 years
    How to add a string to the second list please?