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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Using the capacity property of ArrayList

Using the capacity property of ArrayList

Scheduled Pinned Locked Moved C#
csharpdotnetdata-structurestutorialquestion
4 Posts 4 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.
  • A Offline
    A Offline
    Alex Iagovsky
    wrote on last edited by
    #1

    Hey . As I understood , this property , if set explicitly , should disable the autmatically expantion of the array when using "Add" ,when the Count equals to Capacity .. well .. this doesn't work for me . I just don't want the ArrayList to be expanded automatically when adding item , when Count equals to Capacity. instead I'd expect that it will raise some exception . of course I can do all that in my own class that using the ArrayList , but I'd like that to be done in ArrayList itself . anyone knows how to do it ? Alex ICQ : 10676475 email : alexyag@inter.net.il =================================================== "The harder you fall, the higher you bounce." - American Proverb

    P N 2 Replies Last reply
    0
    • A Alex Iagovsky

      Hey . As I understood , this property , if set explicitly , should disable the autmatically expantion of the array when using "Add" ,when the Count equals to Capacity .. well .. this doesn't work for me . I just don't want the ArrayList to be expanded automatically when adding item , when Count equals to Capacity. instead I'd expect that it will raise some exception . of course I can do all that in my own class that using the ArrayList , but I'd like that to be done in ArrayList itself . anyone knows how to do it ? Alex ICQ : 10676475 email : alexyag@inter.net.il =================================================== "The harder you fall, the higher you bounce." - American Proverb

      P Offline
      P Offline
      Paul Riley
      wrote on last edited by
      #2

      I'm pretty sure Capacity just pre-assigns the memory, so if you have 5 items and a capacity of 10 then it's holding on to the memory for the other 5. But then when you put the 11th item in, your Capacity is increased to 11. What do you actually want to do when item 11 is added? Paul We all will feed the worms and trees
      So don't be shy
      - Queens of the Stone Age, Mosquito Song

      S 1 Reply Last reply
      0
      • A Alex Iagovsky

        Hey . As I understood , this property , if set explicitly , should disable the autmatically expantion of the array when using "Add" ,when the Count equals to Capacity .. well .. this doesn't work for me . I just don't want the ArrayList to be expanded automatically when adding item , when Count equals to Capacity. instead I'd expect that it will raise some exception . of course I can do all that in my own class that using the ArrayList , but I'd like that to be done in ArrayList itself . anyone knows how to do it ? Alex ICQ : 10676475 email : alexyag@inter.net.il =================================================== "The harder you fall, the higher you bounce." - American Proverb

        N Offline
        N Offline
        neroknights
        wrote on last edited by
        #3

        You can fix the size of the ArrayList like so ... // Creates and initializes a new ArrayList. ArrayList myAL = new ArrayList(); myAL.Add( "The" ); myAL.Add( "quick" ); myAL.Add( "brown" ); myAL.Add( "fox" ); myAL.Add( "jumped" ); myAL.Add( "over" ); myAL.Add( "the" ); myAL.Add( "lazy" ); myAL.Add( "dog" ); // Create a fixed-size wrapper around the ArrayList. ArrayList myFixedSizeAL = ArrayList.FixedSize( myAL ); An exception will be thrown whenever yuo add or remove an item from the array. However, you can modify the items currently in myFixedSizeAL.:)

        1 Reply Last reply
        0
        • P Paul Riley

          I'm pretty sure Capacity just pre-assigns the memory, so if you have 5 items and a capacity of 10 then it's holding on to the memory for the other 5. But then when you put the 11th item in, your Capacity is increased to 11. What do you actually want to do when item 11 is added? Paul We all will feed the worms and trees
          So don't be shy
          - Queens of the Stone Age, Mosquito Song

          S Offline
          S Offline
          Stephane Rodriguez
          wrote on last edited by
          #4

          Paul Riley wrote: I'm pretty sure Capacity just pre-assigns the memory Yes.

          public virtual void System.Collections.ArrayList.set_Capacity(int value) {
          object[] local0;

          if (value != (int) this._items.Length) {
          if (value < this._size)
          throw new ArgumentOutOfRangeException(...);
          if (value > 0) {
          local0 = new Object[checked((uint) value)];
          if (this._size > 0)
          Array.Copy(this._items, 0, local0, 0, this._size);
          this._items = local0;
          return;
          }
          this._items = new Object[16];
          }
          }

          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