473,799 Members | 2,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Index out of range

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;t el1++)
{
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
6 11081
How did you allocate the p array?

"Francois Vanderseypen" <Ne***********@ hotmail.com> a écrit dans le message
de news:3f******** *************@r eader0.news.sky net.be...
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;t el1++)
{
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
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.
Nov 13 '05 #3

p.Insert(tel1+t el2,tel2);
"Francois Vanderseypen" <Ne***********@ hotmail.com> wrote in message
news:3f******** *************@r eader0.news.sky net.be...
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;t el1++)
{
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
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;t el1++)
{
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.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #5
Francois Vanderseypen <Ne***********@ hotmail.com> wrote:
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;
}
}


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.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #6
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.

Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2247
by: Craig O'Shannessy | last post by:
Hi all, Just thought I'd mention that I really think this problem needs to be fixed. I I'm patching the 7.4RC1 JDBC drivers as we speak due to this optimiser bug, and it's the third time I've had to do this. I would think this bug causes quite a lot of people to evaluate postgres and decide it has awful primary key performance! I love postgres, and hate to think that this could be happening.
29
5487
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array elements as *(mptr+k) where I've declared MYTYPE *mptr; what should be the type of 'k'? Should it be ptrdiff_t?
2
16132
by: kscdavefl | last post by:
When I run the following code: private void applicationPermissionGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if (e.Item.ItemType == System.Web.UI.WebControls.ListItemType.Item || (e.Item.ItemType == System.Web.UI.WebControls.ListItemType.AlternatingItem)) {
1
2537
by: Clark Choi | last post by:
I ran the sample application called Petstore from msdn. Everything went fine until I tested Update button on the web form to update the database through Datagrid. I have been searching the web to track down this error, but none of them really explain this problem/solution very well. I tried hard to debug this, but debuggin itself was impossible. The error message is as follows:
14
22584
by: micklee74 | last post by:
hi say i have string like this astring = 'abcd efgd 1234 fsdf gfds abcde 1234' if i want to find which postion is 1234, how can i achieve this...? i want to use index() but it only give me the first occurence. I want to know the positions of both "1234" thanks
0
8064
by: ssims | last post by:
I've got a GridView that's sorted by a stored procedure with ROW_NUMBER: PROCEDURE dbo.GetCalendarsByStatusIDPaged ( @startRowIndex int, @maximumRows int, @statusID int ) AS
85
4327
by: Russ | last post by:
Every Python programmer gets this message occasionally: IndexError: list index out of range The message tells you where the error occurred, but it doesn't tell you what the range and the offending index are. Why does it force you to determine that information for yourself when it could save you a step and just tell you? This seems like a "no-brainer" to me. Am I missing something?
2
3762
by: Georgy Panterov | last post by:
I am a relatively new python user. I am writing an economic simulation of acard-game. The simulation runs fine for a few iteration but then it gives an error of "list index out of range." The strange thing is that it will sometimes run for 10 iterations sometimesfor only a few and sometimes won't run at all (seemingly arbitrary). Here is some of the code: for _ in range(100): handA=deal_hand(DECK) #deals 2 'hole' cards from a DECK...
1
3015
by: =?Utf-8?B?SkI=?= | last post by:
Hello As I debug the C# code with a break point and by pressing F11 I eventually get a message stating: ContextSwitchDeadlock was detected Message: The CLR has been unable to transition from COM context 0x17aeb8 to COM context 0x17abd8 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages.
6
18711
by: shashi shekhar singh | last post by:
Respected Sir, I am facing problem when i try to deploy my website on iis 7.0 on test page. i have to display some .mht files on iframe in gridview and error looks like below, Server Error in '/' Application. -------------------------------------------------------------------------------- Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index Description: An unhandled exception...
0
10491
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10268
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10247
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10031
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9079
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7571
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.