473,322 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Array List Question

I have an array list with some values. I need to loop through the arraylist
and change the values. I am using the code below and I am getting an error
(like you would if i looped through a hashtable). Any ideas why?

index = 0;
foreach (object val in arrayList)
{
holeList[index] = (double)val * 2.1;
index++;
}
Nov 15 '05 #1
3 1282
Eric,

You can not change the values in an enumeration while you are
enumerating through them. In order to do this, cycle through the elements,
like this:

for (int pintIndex = 0; pintIndex < arrayList.Count; ++pintIndex)
// Change the value.
arrayList[pintIndex] = ((double) arrayList[pintIndex]) * 2.1;

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"eric" <e@yahoo.com> wrote in message
news:u0**************@TK2MSFTNGP10.phx.gbl...
I have an array list with some values. I need to loop through the arraylist and change the values. I am using the code below and I am getting an error (like you would if i looped through a hashtable). Any ideas why?

index = 0;
foreach (object val in arrayList)
{
holeList[index] = (double)val * 2.1;
index++;
}

Nov 15 '05 #2
eric <e@yahoo.com> wrote:
I have an array list with some values. I need to loop through the arraylist
and change the values. I am using the code below and I am getting an error
(like you would if i looped through a hashtable). Any ideas why?


Yes - because you're modifying the hashtable while using an enumerator,
which you're not allowed to do. The simplest thing to do instead is:

for (int i=0; i < arrayList.Length; i++)
{
arrayList[i] = ((double)arrayList[i])*2.1;
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
If you change a array when you are using the foreach you will get a error because that, you can put the values that you change in other arraylist and later assign the new arraylist to the old arraylist variable without trouble.
--
Bela Istok
MVP C#
Caracas, Venezuela
"eric" <e@yahoo.com> wrote in message news:u0**************@TK2MSFTNGP10.phx.gbl...
I have an array list with some values. I need to loop through the arraylist
and change the values. I am using the code below and I am getting an error
(like you would if i looped through a hashtable). Any ideas why?

index = 0;
foreach (object val in arrayList)
{
holeList[index] = (double)val * 2.1;
index++;
}
Nov 15 '05 #4

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

Similar topics

1
by: jonnytansey2 | last post by:
Can anyone out there give me a pointer regarding creating a dynamically-generated drop-down list connected to an array? And is that question as clear as chocolate spread? Here's what I've got....
5
by: Andrew Dixon - Depictions.net | last post by:
Hi Everyone. Bit new to Java and I have question about arrays. I have wrote some code to look for certain substrings within a larger string which work fine. I would like to store each substring...
6
by: Harry Overs | last post by:
My program needs to take a pointer to BYTE array (unsigned char*) and convert it into a STL list so that each BYTE in the array has its own element in the list, i.e. if the array has hundred bytes...
16
by: herbertF | last post by:
Hi guys, In a program (not my own) I encountered the declaration of a constant pointer to an array consisting of two other const pointers to arrays. Not quite sure why they do it so complicated,...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
4
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving...
4
by: Bill Sun | last post by:
Hi, All I have a conventional question, How to create a 3 dimension array by C language. I see some declare like this: int *** array; int value; array = create3Darray(m,n,l);
7
by: Jim Lewis | last post by:
I'm trying to move a function into pyrex for speed. The python side needs to pass a list to the pyrex function. Do I need to convert to array or something so pyrex can generate tight code? I'm not...
13
by: ANSHUL | last post by:
PLEASE PROVIDE ME D SOLUTION CODE FOR DIS PROBLEM. SELECTION SORT IS BASED ON D FOLLOWING IDEA: SELECTING D LARGEST ARRAY ELEMENT AND SWAPPING IT WITH THE LAST ARRAY ELEMENT LEAVES AN UNSORTED...
1
by: Skyer | last post by:
How to write? I have write program in C language. This program contain 1 array with struct. Every element of table must be dynamic list LIFO (stack). Adding and removing struct variable from...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.