locked
multi dimensional ArrayList

    Question

  • I need to declare and initiate an ArrayList with 10 rows and 4 columns. Here is what i have done

    ArrayList intArray = new ArrayList ();

    intArray.Add(new ArrayList(4) {1,2,7,16});
    intArray.Add(new ArrayList940 {1,6,25,16});

    and so on....

    Im not sure if its the best way to do it. Basically how would you initiate a multi-dimensional ArrayList??

    Many Thanks

    Tuesday, August 19, 2008 11:46 AM

Answers

  • I'm not sure if this is what you are looking for.   It works in VS 2008.

                ArrayList al = new ArrayList() {
                    new ArrayList(){1, 2, 3, 4},
                    new ArrayList(){5, 6, 7, 8},
                };

    If you know it will be an "int" array, why not use ...

        ArrayList list = new ArrayList() {
            new int [] { 1, 2, 3, 4 },
            new int [] { 5, 6, 7, 8 },
        };

    "Boxing" integers is significantly slower than not boxing.  If you plan to read these values a lot, your performance could be improved.


    Les Potter, Xalnix Corporation, Yet Another C# Blog
    • Marked as answer by Solmaz Tuesday, August 19, 2008 1:28 PM
    Tuesday, August 19, 2008 1:20 PM

All replies

  • I'm not sure if this is what you are looking for.   It works in VS 2008.

                ArrayList al = new ArrayList() {
                    new ArrayList(){1, 2, 3, 4},
                    new ArrayList(){5, 6, 7, 8},
                };

    If you know it will be an "int" array, why not use ...

        ArrayList list = new ArrayList() {
            new int [] { 1, 2, 3, 4 },
            new int [] { 5, 6, 7, 8 },
        };

    "Boxing" integers is significantly slower than not boxing.  If you plan to read these values a lot, your performance could be improved.


    Les Potter, Xalnix Corporation, Yet Another C# Blog
    • Marked as answer by Solmaz Tuesday, August 19, 2008 1:28 PM
    Tuesday, August 19, 2008 1:20 PM
  • Thank you very much for your reply. I wasnt sure if that is the correct way of doing it.

    How would I access an element in an ArrayList. For a normal muliti-dimensional array I would use Array[m,n] but I dont know how to address individual elements in ArrayList.

    For example, i want to access the number '5' in the 'al' ArrayList, what index would I use?

    I appreciate the help

    Solmaz
    Tuesday, August 19, 2008 1:28 PM
  • This line will access a member in the list.  It's not too pretty...
                Console.WriteLine("{0}", 
                        ((ArrayList)al[1])[0].ToString());

    This next example does a couple of things.  It prevents you from having to "Box" your integers and it makes for a nicer accessor syntax.
                List<int[]> il = new List<int[]>() {
                    new int[] {1, 2, 3, 4, },
                    new int[] {5, 6, 7, 8, },
                };
                Console.WriteLine("{0}",
                        il[1][0].ToString());


    Les Potter, Xalnix Corporation, Yet Another C# Blog
    Tuesday, August 19, 2008 2:29 PM