SgDotNet
Singapore Professional .NET User Group -For Cool Developers

Array of ArrayList

rated by 0 users
This post has 7 Replies | 1 Follower

Top 50 Contributor
Posts 44
Psychic Posted: 10-02-2005 11:40 AM
Just curious.  Is it weird to create an array of ArrayList? I have a few arrayList of varying size so I am thinking of storing it this way so that I can use a loop to process it.  I dun think I shld be using a 2D array since the arrays are of different length.
Top 25 Contributor
Posts 154

it's not weird, but it's usually bad programming.

C# supports jagged arrays. so if the array array lengths are fixed, you should make use of it instead of using ArrayLists due to type unsafety and excessive runtime overhead.

http://feelite.com/blog
Top 50 Contributor
Posts 44
 feelite wrote:

it's not weird, but it's usually bad programming.

C# supports jagged arrays. so if the array array lengths are fixed, you should make use of it instead of using ArrayLists due to type unsafety and excessive runtime overhead.

 

Unfortunately the arrays I have are of varying length.

Top 25 Contributor
Posts 154

varying length is not a problem, the new opreator will auto-allocate the heap memory for you, e.g.

int[][] jagged = new int[2][];

jagged[0] = new int[len1];
jagged[1] = new int[len2];

however, if the array will grow or contract during runtime, then you may have to use arraylist. :(

http://feelite.com/blog
Not Ranked
Posts 1
And what about, f.e., jagged arraylist, arraylist of arraylists, or arraylist tree. See the foolowing code:

/*
 * project      : jagged arraylist
 *
 * author       : xxxxx0
 *
 * location     : czech republic, europe (european union)
 *
 * created      : saturday, february 04, 2006
 *                05:31 pm utc+1
 *
 * language     : programming, c# (microsoft visual c sharp 2005 express)
 *                comments, english
 *                messages, english
 *
 * version      : 1.0
 *
 * description  : this sample code shows how to create jagged n-dimensional arraylist,
 *                and how the jagged n-dimensional arraylist works
 *
 *
 */


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // define array lists
            System.Collections.ArrayList a0 = new System.Collections.ArrayList();
            System.Collections.ArrayList a1 = new System.Collections.ArrayList();
            System.Collections.ArrayList a2 = new System.Collections.ArrayList();
            System.Collections.ArrayList a3 = new System.Collections.ArrayList();

            // insert the test values, syntax of unique values is as follows:
            //
            //   <a>
            //   {arraylist_index=}[<1>..<3>]
            //   <, >
            //   {arraylist_letter=}[<a>..<c>]
            //   {arraylist_item_letter=}[<a>..<c>]
            //

            // insert something to arraylist a1
            a1.Add("a1, aa");
            a1.Add("a1, ab");
            a1.Add("a1, ac");
            // insert something to arraylist a2
            a2.Add("a2, ba");
            a2.Add("a2, bb");
            a2.Add("a2, bc");
            // insert something to arraylist a3
            a3.Add("a3, ca");
            a3.Add("a3, cb");
            a3.Add("a3, cc");

            // insert arraylist a1, a2, a3 to arraylist a0
            // arraylist a0 is arraylist of arraylists a1, a2, a3
            a0.Add(a1);
            a0.Add(a2);
            a0.Add(a3);

            // show contents of arralist a0
            {
                Console.WriteLine("\nshow contents of arraylist a0\n");
                System.Collections.IEnumerator a0e = a0.GetEnumerator();
                while (a0e.MoveNext())
                {
                    Console.WriteLine(a0e.Current);
                }
            }

            // show contents of arraylist a0 arraylists
            {
                Console.WriteLine("\nshow contents of arraylist a0 arraylists\n");
                System.Collections.IEnumerator a0e = a0.GetEnumerator();
                while (a0e.MoveNext())
                {
                    System.Collections.ArrayList a0eaX = (System.Collections.ArrayList)a0e.Current;
                    System.Collections.IEnumerator a0eaXe = a0eaX.GetEnumerator();
                    Console.WriteLine("\nMoveNext...\n");
                    while (a0eaXe.MoveNext())
                    {
                        Console.WriteLine(a0eaXe.Current);
                    }
                }
            }

            // keep the screen output until enter pressed
            Console.ReadLine();
        }
    }
}

When you run this, you will get following:

show contents of arraylist a0

System.Collections.ArrayList
System.Collections.ArrayList
System.Collections.ArrayList

show contents of arraylist a0 arraylists


MoveNext...

a1, aa
a1, ab
a1, ac

MoveNext...

a2, ba
a2, bb
a2, bc

MoveNext...

a3, ca
a3, cb
a3, cc

Top 25 Contributor
Posts 154
sure, if careful analysis points to arraylists of arraylists as the only reasonable choice data structure, then by all means go ahead with it.

from my experience, such a data structure is usually not the optimal choice
http://feelite.com/blog
Top 10 Contributor
Posts 2,284
 feelite wrote:
from my experience, such a data structure is usually not the optimal choice
It is ok to make use of jagged arrays so as long as there is (sufficient) meaning and context put to it. The problem most programmers create is a false sense of meaning to just what those arrays/ArrayLists are actually meant to contain.

Using variables like a0eaX, a1, a2 tell me absolutely nothing of their purpose and use. Doing so only makes it no more than an academic programming exercise, unfit for real world use (which unfortunately happens in real world too often). Variable naming is the first and foremost fundamental that needs to be addressed, then hierarchical data structures won't get that confusing.

The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral

Top 100 Contributor
Posts 22

The <H01<E of variable names in the sample code sure is beri the interesting.

Page 1 of 1 (8 items) | RSS
Copyright SgDotNet 2004-2008
Powered by Community Server (Commercial Edition), by Telligent Systems