Index out of range

  • Thread starter Francois Vanderseypen
  • Start date
F

Francois Vanderseypen

This nested 'for' gives an out of range error on the second pass of the
second loop, though it looks allright. Isn't it wierd?

for (int tel1=0;tel1<3;tel1++)
{
for (int tel2=0;tel2<15;tel2++)
{
p[tel1+tel2]=tel2;
}
}

Initializing the second loop with tel2=1 gives immediately an error...I have
no clue.

Thanks for any hint.
 
F

Francois Vanderseypen

The p ArrayList is as follows:

ArrayList p=new ArrayList(45);
for (int tel1=0;tel1<3;tel1++)
{
for (int tel2=1;tel2<15;tel2++)
{
p[tel1+tel2]=tel2;
}
}



Setting ..=new ArrayList(200) or ...=new ArrayList() does not change
anything.
 
J

Jon Skeet

Francois Vanderseypen said:
This nested 'for' gives an out of range error on the second pass of the
second loop, though it looks allright. Isn't it wierd?

for (int tel1=0;tel1<3;tel1++)
{
for (int tel2=0;tel2<15;tel2++)
{
p[tel1+tel2]=tel2;
}
}

Initializing the second loop with tel2=1 gives immediately an error...I have
no clue.

Well, how large is the "p" array? If you could post a short but
*complete* example which demonstrates the problem, that would help...
See http://www.pobox.com/~skeet/csharp/complete.html for what I mean.
 
J

Jon Skeet

Francois Vanderseypen said:
The p ArrayList is as follows:

ArrayList p=new ArrayList(45);
for (int tel1=0;tel1<3;tel1++)
{
for (int tel2=1;tel2<15;tel2++)
{
p[tel1+tel2]=tel2;
}
}

Ah. I'd assumed p was an array here. (This is why posting a short but
complete program to start with saves time.) You need to either fill the
list with values first, or possibly create an array and add all of that
to the list afterwards.
 
A

Adrian Vinca [MSFT]

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/default.asp?url=/library/en-us/cpref/html/
frlrfsystemcollectionsarraylistclasscapacitytopic.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/default.asp?url=/library/en-us/cpref/html/
frlrfsystemcollectionsarraylistclasscapacitytopic.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;tel1++)
{
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;tel1++)
{
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;tel1++)
{
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top