Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Dynamic Array

Dynamic Array

Scheduled Pinned Locked Moved C#
data-structureshelptutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hoang Dung
    wrote on last edited by
    #1

    Hi, How to I allocate a dynamic array in run time. I want to allocate an array, but length of array unfixed. Please help me. Thanks. H.Dung

    F M 2 Replies Last reply
    0
    • H Hoang Dung

      Hi, How to I allocate a dynamic array in run time. I want to allocate an array, but length of array unfixed. Please help me. Thanks. H.Dung

      F Offline
      F Offline
      Frank Olorin Rizzi
      wrote on last edited by
      #2

      Dung, what do you mean by "length of array unfixed" ? If you mean that you will not know the length until run-time, consider the following snipplet: int x = 3; char[] myArray = new char[x]; This compiles, and thus, even if x was determine by picking user input (or cfg file, or what have you), you should be able to use it. If, on the other hand, you need an array whose size will change in time (i.e. starts with 10 elements in it, but later on you need to add 5 more, and then shrink it to 5, etc etc) then arrays are not the correct data structure for you. Arrays have "fixed size" (they do not grow in time), but if you look at the classes in System.Collections you should find something for you. I often use the Hashtable class (but it's often an overkill!). HTH, Frank

      1 Reply Last reply
      0
      • H Hoang Dung

        Hi, How to I allocate a dynamic array in run time. I want to allocate an array, but length of array unfixed. Please help me. Thanks. H.Dung

        M Offline
        M Offline
        Meysam Mahfouzi
        wrote on last edited by
        #3

        You can simply use ArrayList class. Use its Add and Remove methods. this way, you don't need to care about array size, it's completely dynamic I hope this helps :rose:


        Don't forget, that's

        Persian Gulf

        not Arabian gulf!

        H 1 Reply Last reply
        0
        • M Meysam Mahfouzi

          You can simply use ArrayList class. Use its Add and Remove methods. this way, you don't need to care about array size, it's completely dynamic I hope this helps :rose:


          Don't forget, that's

          Persian Gulf

          not Arabian gulf!

          H Offline
          H Offline
          Hoang Dung
          wrote on last edited by
          #4

          Hi, Thank you very much. I have been used the ArrayList class in System.Collections namespace, and I have everything which I need. H.Dung.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups