using Arrays with unknown size

Hi,

I need to know how can I using an array or arrayList if I don't know the number of elements in advance

If I can't , is there an alternative way

Thanks,

Aya.



Answer this question

using Arrays with unknown size

  • Alan Glazer

    To change the capacity of an array, you can write lots of code to re-allocate arrays with different sizes, but this has already been done for you.

    If you don't know the capacity beforehand, the ArrayList is your best bet (with a slight performace panelty). The ArrayList dynamically resizes when you try to fill it up beyond its capacity.

    To control the size, you can set the Capacity property, or use TrimToSize().



  • DimTim

    For ArrayList you don't need to know the size in advance. Just create an ArrayList and add items to it:

    ArrayList list = new ArrayList();

    list.Add("Hello");

    list.Add("World");

    list.Add("!");


  • using Arrays with unknown size