Using the capacity property of ArrayList
-
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
-
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
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 -
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
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.:)
-
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 SongPaul 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];
}
}