By passing that parameter to the constructor, you specify the capacity of
the list (the number of elements that the new list is initially capable of
storing). The actual count of elements remains zero. You get an exception
because you are trying to access an element from the list, but your list is
still empty.
For more details see:
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemcoll ectionsarraylis tclasscapacityt opic.asp
"Capacity is the number of elements that the ArrayList is capable of
storing. 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 of the list is doubled by
automatically reallocating the internal array.
When the value of Capacity is set explicitly, the internal array is also
reallocated to accommodate the specified capacity. If Capacity is
explicitly set to zero, the common language runtime sets it to the default
capacity instead. The default capacity is 16."
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemcoll ectionsarraylis tclasscapacityt opic.asp
"If Count is less than Capacity, this method is an O(1) operation. If the
capacity needs to be increased to accommodate the new element, this method
becomes an O(n) operation, where n is Count."
The solution to your problem is to add elements in list before accessing
them.
You can add them all at once:
ArrayList p=new ArrayList(45);
p.AddRange (new int[45]); //add 45 elements in the list by adding an array
of elements; these elements are initialized to 0
for (int tel1=0;tel1<3;t el1++)
{
for (int tel2=1;tel2<15; tel2++)
{
p[tel1+tel2]=tel2;
}
}
or you can add them one by one:
ArrayList p=new ArrayList(45);
for (int i=0; i< 45; i++)
{
p.Add (0); //I assume that the default value is 0
}
for (int tel1=0;tel1<3;t el1++)
{
for (int tel2=1;tel2<15; tel2++)
{
p[tel1+tel2]=tel2;
}
}
The p ArrayList is as follows:
ArrayList p=new ArrayList(45);
for (int tel1=0;tel1<3;t el1++)
{
for (int tel2=1;tel2<15; tel2++)
{
p[tel1+tel2]=tel2;
}
}
Setting ..=new ArrayList(200) or ...=new ArrayList() does not change
anything.
--
Adrian Vinca, Developer Division
This posting is provided "AS IS" with no warranties, and confers no rights.
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.