Dynamic arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dusty

    #1

    Dynamic arrays

    How can I make my dynamic array ? I'm curios how the
    ArrayList does it.
    For example:
    string[] s = new string[16];

    and later I want to expand this array to more than 16
    elements without loosing previous data (like ReDim in
    VB).

    Any ideas ?



  • Mattias Sjögren

    #2
    Re: Dynamic arrays

    [color=blue]
    >Any ideas ?[/color]

    Allocate a new, larger array, copy over the existing elements, and
    drop the reference to the old array. That's what ReDim does, and I'm
    guessing that the ArrayList does something similar.



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org

    Please reply only to the newsgroup.

    Comment

    • Val Savvateev

      #3
      Re: Dynamic arrays

      If you look at an ArrayList instaince in the debugger's Watch window you'll
      descover that it has an internal (private?) member called _items and it's
      just an array of Objects. So I would imagine Mattias' guess is correct -
      ArrayList allocates/reallocates regular arrays behind the scenes, however
      that process may be optimized in a way...

      Val.

      "Dusty" <dusty@ptt.yu > wrote in message
      news:%234398UiV DHA.3924@tk2msf tngp13.phx.gbl. ..[color=blue]
      > How can I make my dynamic array ? I'm curios how the
      > ArrayList does it.
      > For example:
      > string[] s = new string[16];
      >
      > and later I want to expand this array to more than 16
      > elements without loosing previous data (like ReDim in
      > VB).
      >
      > Any ideas ?
      >
      >
      >[/color]


      Comment

      • Uriel Julio [MSFT]

        #4
        Re: Dynamic arrays

        Hello,

        ArrayList is a dynamic array. You should add elements to it by using its
        Add method. There are two properties that you can check to get its size:
        .Count and .Capacity. The capacity will always be equal to or higher than
        the count. You could think of it as similar to a vector class. If you want
        to trim it so that the capacity is the count, you can call its TrimToSize()
        method.

        I hope that helped.

        --------------------[color=blue][color=green]
        >>From: Mattias Sjögren <mattias.dont.w ant.spam@mvps.o rg>
        >>Subject: Re: Dynamic arrays
        >>Date: Wed, 30 Jul 2003 01:31:02 +0200
        >>References: <#4398UiVDHA.39 24@tk2msftngp13 .phx.gbl>
        >>X-Newsreader: Forte Agent 1.8/32.548
        >>MIME-Version: 1.0
        >>Content-Type: text/plain; charset=ISO-8859-1
        >>Content-Transfer-Encoding: 8bit
        >>Message-ID: <egtc0liVDHA.23 28@TK2MSFTNGP12 .phx.gbl>
        >>Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
        >>NNTP-Posting-Host: g150.ryd.studen t.liu.se 130.236.238.150
        >>Lines: 1
        >>Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP12.phx.g bl
        >>Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1728 23
        >>X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
        >>
        >>[color=darkred]
        >>>Any ideas ?[/color]
        >>
        >>Allocate a new, larger array, copy over the existing elements, and
        >>drop the reference to the old array. That's what ReDim does, and I'm
        >>guessing that the ArrayList does something similar.
        >>
        >>
        >>
        >>Mattias
        >>
        >>--
        >>Mattias Sjögren [MVP] mattias @ mvps.org
        >>http://www.msjogren.net/dotnet/
        >>Please reply only to the newsgroup.
        >>[/color][/color]


        --

        This posting is provided "AS IS" with no warranties, and confers no rights.
        Use of included script samples are subject to the terms specified at
        http://www.microsoft.com/info/cpyright.htm

        Note: For the benefit of the community-at-large, all responses to this
        message are best directed to the newsgroup/thread from which they
        originated.

        Comment

        Working...