Tutorials to .com

Tutorials to .com » Dotnet » Winform » Traversal ArrayList pitfalls

Traversal ArrayList pitfalls

Print View , by: iSee ,Total views: 19 ,Word Count: 1858 ,Date: Tue, 25 Aug 2009 Time: 10:16 AM

Traversal ArrayList pitfalls
Scene:

In the ArrayList qualified records deleted, the first time to write the procedures are as follows:

foreach (string aStr in aList)
(
if (aStr.Equals (textBox1.Text))
(
aList.Remove (aStr);
)
)
Seems to be no errors, compiled through, but the run-time if the conditions encountered meet the data will throw an error:

Click here to open the picture in a new window
Simple solution is to do? Then with the Clone method is best, however, and with the following code:

ArrayList bList = (ArrayList) aList.Clone ();
foreach (string aStr in bList)
(
if (aStr.Equals (textBox1.Text))
(
aList.Remove (aStr);
)
)

It seems there will be a collection of types such problems.

posted on 2004-09-08 13:53 the wind before the flocculation ~ ~ Read (1047) Comments (25) Editor collection

Comments
# Re: traversal ArrayList easy to make mistakes
Used for such problems can be avoided, but also for the implementation of the efficiency is also higher than the foreach
2004-09-08 14:10 | do not know anything
# Re: traversal ArrayList easy to make mistakes
Ah, you actually ... ..... How dare you use foreach to reomove, this is the problem ..... the notorious Collection
2004-09-08 14:24 | Hanfeng days injury
# Re: traversal ArrayList easy to make mistakes
I generally use
foreach (object item in al.ToArray ())
..
Or in new ArrayList (somecollection).

2004-09-08 14:55 | Lostinet
# Re: traversal ArrayList easy to make mistakes
As if there is a shallow copy method.
The best is what, do not use Clone
2004-09-08 15:11 | hyifeng
# Re: traversal ArrayList easy to make mistakes
Reverse traversal can be used for statement
2004-09-08 16:07 | feilng
# Re: traversal ArrayList easy to make mistakes
I have also made such a mistake, He He
2004-09-08 16:10 | cure
# Re: traversal ArrayList easy to make mistakes
To what do not know:
for the method can also be ah, the code is as follows?
for (int i = 0; i <aList.Count; i + +)
if (aList [i]. Equals (textBox1.Text))
aList.RemoveAt (i);

To Lostinet:
Your method were good too

To hyifeng:
For the ArrayList, Clone is a shallow copy of the. MemberwiseClone do you say yes?

This treatment seems to have been three methods of, foreach inside with Clone, for loop, ToArray, did not know that a better arrangement?
How do I get more information?

2004-09-08 16:12 | the wind before the flocculation ~ ~
# Re: traversal ArrayList easy to make mistakes
To feilng:

Reverse traversal? What good ah? Generally how to achieve it?
2004-09-08 16:18 | the wind before the flocculation ~ ~
# Re: traversal ArrayList easy to make mistakes
I have had, for should be the most safe and easy to understand

Clone and increased the handling capacity of at least ToArray
2004-09-08 16:23 | dali
# Re: traversal ArrayList easy to make mistakes
To the wind before the flocculation ~ ~:
For the for the code, should be added a conditional branch, call the RemoveAt method, then, index can not be increased. That loop increment should not be written in the for statement, the parentheses inside.
2004-09-08 17:56 | FantasySoft
# Re: traversal ArrayList easy to make mistakes
Can not stand up to you, actually. . . . Even such a low-level errors are guilty! ! ! ! One also does not matter, even a group! ! ! Halo ~

Delete the code is as follows:


for (int i = aList.Count; - i> = 0;)
if (aList [i]. Equals (textBox1.Text))
aList.RemoveAt (i);
2004-09-08 18:54 | Old-winged
# Re: traversal ArrayList easy to make mistakes
~
2004-09-08 19:20 | hBifTs
# Re: traversal ArrayList easy to make mistakes
To the old wing:

~ ~ words can not say, and programming where there will be no mistake? Otherwise there will not be a BUG.

Thank you paste the code, I tried, and no problem, can be said for a way.

To FantasySoft:

I have that for the code is adopted, it may achieve the desired effect, please indicate me the error code, thanks!
2004-09-08 19:58 | the wind before the flocculation ~ ~
# Re: traversal ArrayList easy to make mistakes
To the wind before the flocculation ~ ~: the code itself is not wrong syntax, but there is a logical question. Because the RemoveAt method to change the Count method to return value, and causing each element are not traversed to. It is also proposed feilng and the old wing reverse traversal reasons.
See the following code:
using System.Collections;
using System;
class Test
(
public static void Main ()
(
ArrayList test = new ArrayList ();
for (int i = 0; i <3; i + +)
test.Add ( "test");
test.Add ( "testAgain");
test.Add ( "testAgain");
for (int i = 0; i <test.Count; i + +)
(
if (test [i]. Equals ( "test"))
test.RemoveAt (i);
)
for (int i = 0; i <test.Count; i + +)
Console.WriteLine (test [i]);
)
)

test of this ArrayList inside the "test" Should not have been remove out then? In fact, if written like this, or will be left by one.
2004-09-09 02:22 | FantasySoft
# Re: traversal ArrayList easy to make mistakes
To FantasySoft:

Very grateful! The original code because each is different, and therefore did not find the problem.
Under the careful thought, indeed, as you said: Count changed, making index = 0 to be omitted.

The only way to appear for a reverse traversal.

Availability using three different methods under ILDASM read the generated code, compare the corresponding function, and found that the maximum stack use the same code for different lengths, for the shortest, ToArray () middle, Clone up.

Way forward for a statement can be added, but looks bloated than the reverse: the
for (int i = 0; i <aList.Count; i + +)
if (aList [i]. Equals (textBox1.Text))
(
aList.RemoveAt (i);
i -;
)
2004-09-09 09:18 | the wind before the flocculation ~ ~
# Re: traversal ArrayList easy to make mistakes
To the wind before the flocculation ~ ~: You can write
for (int i = 0; i <aList.Count;)
if (aList [i]. Equals (textBox1.Text))
(
aList.RemoveAt (i);
)
else
i + +;
This writing is not even clear out? for the incremental statements do not have to write for inside the brackets.

2004-09-09 10:24 | Fantasysoft
# Re: traversal ArrayList easy to make mistakes
That's right, but also a way! ^ _ ^
2004-09-09 10:36 | the wind before the flocculation ~ ~
# Re: traversal ArrayList easy to make mistakes
With respect, to see Louzhu your "carefully to the next, indeed, as you said: Count changed, making index = 0 to be missed." This phrase, feel you do not really understand why the House will have Top issues

Been omitted is not the original list of the 0 number item, but the No. 1 item

Why? Because when you remove the first i-item, the first [i + 1, count) in the domain of the index value of all items are reduced by one (Array is continuous, to meet long as there is k ∈ [0, count) then Array [k] must exist), then when you are in the loop with the next one i + 1 for the index visit, that is the original list in the index for the i + 2 of that item, can be seen, in this case, No. 0 is deleted, the original No. 1 becomes the new No. 0, the former No. 2 becomes the new No. 1. . . The next round of visits is the No. 1 the previous No. 2, while the original has not been No. 1 will always have access to

Understand this natural right of the iterative method can be drawn, is when you should pay attention to the index sequence and the possible termination of changes in conditions, when the reverse is simpler
2004-09-09 14:44 | man with a problem
# Re: traversal ArrayList easy to make mistakes
To man with a problem:

Oh ~ ~ Sorry, I estimate the expression of a problem.
index = 0 is changed after the Count, and it is indeed the original index = 1, as you said.

Thank correction!
2004-09-09 15:16 | the wind before the flocculation ~ ~
# Re: traversal ArrayList easy to make mistakes
re: traversal ArrayList easy to make mistakes
Reverse traversal can be used for statement
2004-09-08 16:07 | feilng

Totally agree, I think this is the best way of
ps this kind of thing, without a fault would be a mistake for the first time, ha ha
2004-09-09 16:56 | myrat
# Re: traversal ArrayList easy to make mistakes
Take a look at this code, what result?

int [] myarray = (1,2,3);

foreach (int num in myarray) (
num + +;
)
2004-09-09 17:21 | juqiang
# Re: traversal ArrayList easy to make mistakes
To juqing:

Interestingly, ah should not do

-------------------------------------------------- ------------------------------
MSDN in the description:
foreach statement for an array or object in the collection of embedded statements for each element of duplication of a group. foreach statement is used to iterate through the collection to obtain the required information, but should not be used to change the contents of the collection to avoid unpredictable side effects.

-------------------------------------------------- ------------------------------

num + + is equivalent to num = num + 1, modify the content.
Compile an error message:
... (201): Can not assigned to the "num", because it is read-only

2004-09-09 17:33 | the wind before the flocculation ~ ~
# Traversal ArrayList easy to make mistakes [TrackBack]
Ping Back From: blog.csdn.net
windsails reference to the article, Address: http://blog.csdn.net/windsails/archive/2004/09/10/100331.aspx
2004-09-10 13:21 | windsails
# Re: traversal ArrayList easy to make mistakes
Why reverse?

int i = 0;
while (i <aList.Count)
(
if (aList [i]. Equals (textBox1.Text))
(
aList.RemoveAt (i)
continue;
)
i + +;
)

2004-10-20 21:29 | Popeye the Sailor
# Re: traversal ArrayList easy to make mistakes
Is simple and efficient reverse-ah, aList.Count initialization only use them once, other conditions should be used every time ah. Although aList.Count is also variable, but from the perspective of machine code, an indirect address reference still has to spend a lot more than the register reference to time.
2004-12-09 12:45 | Unknown


.Net Windows development Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.