C# Programming - Collection Classes - Discussion
Discussion Forum : Collection Classes - General Questions (Q.No. 11)
11.
Suppose value of the Capacity property of ArrayList Collection is set to 4. What will be the capacity of the Collection on adding fifth element to it?
Discussion:
9 comments Page 1 of 1.
Ankuj kumar said:
8 years ago
Arraylist is growable and shrinkable dynamically when the defined size is full it automatically double of its existing size.
(1)
PriTi S. said:
8 years ago
Count property tells how many elements are currently in ArrayList whereas Capacity property tells how many elements can fit into ArrayList without allocating more memory.
When you add an element to List it will check the Capacity whether the element can fit or not. If not it will just " Pre-Allocate" the ArrayList capacity to double its current Capacity.
So in our example, capacity is 4 since when the first element is added Initial Capacity will be set to 4. After adding a 5th element into the List, Capacity will be 8 and so on.
When you add an element to List it will check the Capacity whether the element can fit or not. If not it will just " Pre-Allocate" the ArrayList capacity to double its current Capacity.
So in our example, capacity is 4 since when the first element is added Initial Capacity will be set to 4. After adding a 5th element into the List, Capacity will be 8 and so on.
Krishna said:
9 years ago
Capacity is the number of elements that the ArrayList can store. Count is the number of elements that are actually in the ArrayList. Capacity is always greater than or equal to Count. If Count exceeds Capacity while adding elements, the capacity is automatically increased by reallocating the internal array before copying the old elements and adding the new elements.
Krishna said:
9 years ago
private void EnsureCapacity(int min)
{
if (this._items.Length < min)
{
int num = (this._items.Length == 0) ? 4 : (this._items.Length * 2);
if (num < min)
{
num = min;
}
this.Capacity = num;
}
}
{
if (this._items.Length < min)
{
int num = (this._items.Length == 0) ? 4 : (this._items.Length * 2);
if (num < min)
{
num = min;
}
this.Capacity = num;
}
}
Samir said:
10 years ago
ArrayList a = new ArrayList(4);
a.Add(1);
a.Add(2);
a.Add(3);
a.Add(4);
foreach (int i in a)
Console.WriteLine(" " + i);
Console.WriteLine(a.Capacity);
a.Add(5);
Console.WriteLine(a.Capacity);
==========================
1 st size array is 4 by default.
5 th element is inserted 4 is incerease.
9 th element is inserted 4 is incerease.
a.Add(1);
a.Add(2);
a.Add(3);
a.Add(4);
foreach (int i in a)
Console.WriteLine(" " + i);
Console.WriteLine(a.Capacity);
a.Add(5);
Console.WriteLine(a.Capacity);
==========================
1 st size array is 4 by default.
5 th element is inserted 4 is incerease.
9 th element is inserted 4 is incerease.
Joe said:
10 years ago
This explanation does not clearly show how to get the index 8. Can you be more specific?
Shivani said:
10 years ago
I can't understand please any one explain briefly?
ManiKrishna said:
1 decade ago
The elements are stored in the following procedure in the array list:
arraylist[0] = 1st element.
arraylist[1] = 2nd element.
arraylist[2] = 3rd element.
arraylist[4] = 4th element.
arraylist[8] = 5th element.
arraylist[0] = 1st element.
arraylist[1] = 2nd element.
arraylist[2] = 3rd element.
arraylist[4] = 4th element.
arraylist[8] = 5th element.
Aritra Roy said:
1 decade ago
Can you please explain the logic behind this?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers