By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,666 Members | 1,413 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,666 IT Pros & Developers. It's quick & easy.

Index out of range

P: n/a
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.
Nov 13 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
How did you allocate the p array?

"Francois Vanderseypen" <Ne***********@hotmail.com> a écrit dans le message
de news:3f*********************@reader0.news.skynet.b e...
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.

Nov 13 '05 #2

P: n/a
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.
Nov 13 '05 #3

P: n/a

p.Insert(tel1+tel2,tel2);
"Francois Vanderseypen" <Ne***********@hotmail.com> wrote in message
news:3f*********************@reader0.news.skynet.b e...
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.

Nov 13 '05 #4

P: n/a
Francois Vanderseypen <Ne***********@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #5

P: n/a
Francois Vanderseypen <Ne***********@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #6

P: n/a
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/
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/de...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.

Nov 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.