Create an Unknown Number of Arraylists

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy in S. Jersey

    #1

    Create an Unknown Number of Arraylists

    I would like to create an unknown number of ArrayLists, that is I don't know
    that until runtime.

    Of course I can do this if I knew I needed two:

    ArrayList IntervalArray1 = new ArrayList();
    ArrayList IntervalArray2 = new ArrayList();

    but what if I don't know how many I need at build time?

    Although the code below doesn't work, it expresses sorta what I would like
    to do

    for (int j = 0; j < onereader.Field Count; j++)
    {
    ArrayList IntervalArray[j] = new ArrayList();
    }

    Any ideas? Some code to do it would be nice.

    Thx


  • Arne Vajhøj

    #2
    Re: Create an Unknown Number of Arraylists

    Andy in S. Jersey wrote:
    I would like to create an unknown number of ArrayLists, that is I don't know
    that until runtime.
    >
    Of course I can do this if I knew I needed two:
    >
    ArrayList IntervalArray1 = new ArrayList();
    ArrayList IntervalArray2 = new ArrayList();
    >
    but what if I don't know how many I need at build time?
    Make an ArrayList of ArrayList's.

    ArrayList IntervalArrays = new ArrayList();

    and then at runtime N times:

    IntervalArrays. Add(new ArrayList());

    and use it as:

    IntervalArrays[arrayno].Add(something) ;

    and

    IntervalArrays[arrayno][itemno]

    Arne

    Comment

    • Laurent Bugnion

      #3
      Re: Create an Unknown Number of Arraylists

      Hi,

      Arne Vajhøj wrote:
      Andy in S. Jersey wrote:
      >I would like to create an unknown number of ArrayLists, that is I
      >don't know that until runtime.
      >>
      >Of course I can do this if I knew I needed two:
      >>
      >ArrayList IntervalArray1 = new ArrayList();
      >ArrayList IntervalArray2 = new ArrayList();
      >>
      >but what if I don't know how many I need at build time?
      >
      Make an ArrayList of ArrayList's.
      >
      ArrayList IntervalArrays = new ArrayList();
      >
      and then at runtime N times:
      >
      IntervalArrays. Add(new ArrayList());
      >
      and use it as:
      >
      IntervalArrays[arrayno].Add(something) ;
      >
      and
      >
      IntervalArrays[arrayno][itemno]
      >
      Arne
      You need to cast the content of ArrayLists before you use them, because
      the content is defined as "object".

      That would be:

      ( (ArrayList) IntervalArrays[arrayno] ).Add(something );

      and

      ( (ArrayList) IntervalArrays[arrayno] )[itemno];

      which is cumbersome to say the least. That's why you should rather use
      List<if you can

      List<ArrayListi nternalArrays = new List<ArrayList> ();

      HTH,
      Laurent
      --
      Laurent Bugnion, GalaSoft
      Software engineering: http://www.galasoft-LB.ch
      Private/Malaysia: http://mypage.bluewin.ch/lbugnion
      Support children in Calcutta: http://www.calcutta-espoir.ch

      Comment

      Working...