473,396 Members | 1,853 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,396 software developers and data experts.

best way to enumerate List<> & remove unwanted elements?

Obviously you can't just use a simple for loop, since you may skip
over elements.

You could modify the loop counter each time an element is deleted.
But, the loop ending condition must be checked on each iteration,
since the Count changes as you delete elements. I would think it is
guaranteed to be computed each time, and not cached.

So, is this the best way?

List<intmylist = .......something......;
for (int i=0; i<mylist.Count; i++) {
if (want_to_remove) {
mylist.RemoveAt(i);
i--;
}
}

Zytan

May 13 '07
56 5081
......somehow you got it into your head that
what I told you what you were doing was wrong.
All I said is that you shouldn't use (i) the way you were
doing it, because that could lead to the loop blowing.
Please explain the logic of the above.

Me and everyone else:
loop could blow == doing something wrong

You:
loop could blow != doing something wrong
Please, let's forget about your little loop example. Let's just take dealing
with the idx counter on the loop, period.
This entire discussion is about the loop. If you want to talk about
something else, start another thread.
Under normal circumstances, one doesn't mess with the count idx. It's a best
programming practice that's used by many.
Yes, because it used to be the CX register, and if you changed the
variable, CX didn't change. I agree. But, times have changed. I
agree that it's still ugly, but mostly due to the past. Since, it is
guaranteed to work today, so we are allowed to change it. Still ugly,
yes, since it's the loop iterator, and it should be left to do what it
should do. I agree.
Most use another variable an indirect method to control the loop, if they
need a way of terminating out of the loop based on the iteration count.
Yes, I agree.
So your way works
Yes, thank you. So my loop will not blow. That's what I thought. We
are now all in agreement.
>, big deal.
Sorry, but you're the one making it a big deal. That's what happens
when you say legitimate code is faulty. You said it would blow. It
wouldn't. It's just ugly code, not wrong. Two different things.
Your way would simply not be the way I would do
it. I would never let your way come through a code review if you were
setting in front of me.
I agree. That's EXACTLY why I asked to find the better way.
It's a bad habit a lazy habit of programming, that could lead to trouble in
the long run in other areas where you worked a project.
I agree.
I would never directly start controlling the idx counter of a loop. the way
you're doing it.
Me neither.
Your way should never hit the light of day IMHO. Is it wrong, no it's not
wrong. Is it a best practice, no it's not a best practice either.
My way is legitimate code that will never blow up. But it's ugly
code, since it hides the what the loop iterator does, since it is
changed inside the loop. So, it's horrible practive. Thus my search
for a better solution. Thanks to this group, I found it.
That's just the way I was taught.
You were taught well.
This will be my last post on this subject, because I am through with you and
anybody else that needs to make a comment. There will not be a return reply.
It won't be read by me, because I am moving on.
Please, stop with the drama.

Zytan

May 16 '07 #51
Arnold wrote:
All I said is that you shouldn't use (i) the way you were
doing it, because that could lead to the loop blowing.
Arnold wrote:
Is the boy's code going to blow, no.
Arnold, everyone including me says my original code sucks. This
entire discussion was about your statement saying the code would
blow. Now that you admit that your original statment, the basis of
the entire discussion, was wrong, we are ALL on the same page.

Thank you.

Zytan

May 16 '07 #52

"Zytan" <zy**********@gmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
Arnold wrote:
>All I said is that you shouldn't use (i) the way you were
doing it, because that could lead to the loop blowing.

Arnold wrote:
>Is the boy's code going to blow, no.
Maybe, what I should have said out of the gate was *that your coding was
horrible enough with messing with the idx that the approach could lead to a
blow up with something else you might be trying to do in the future when
jacking around with an idx like that in a loop*.
Really, that's what stood out to me (the red flag) was that idx manipulation
and not the intent of the little loop, which if you will recall, you asked
didn't I understand what the loop was doing. Well, all I saw was the idx
being manipulated with you in the meantime doing something with the idx in
another statement.

It was not until later with ones that needed to post slobbering at the mouth
about the horrible little loop and how it wasn't going to blow up, that this
thread spun out of control, along with the .Net buggy nonsense as well being
thrown in my face.

I mis-spoke about the intend of my statement and the blow up condition,
which I should have clarified my statement right out of the gate, to avoid
the pitiful and trifling nonsense that was displayed to me here in this
thread.

And if you think I read every post that was being made to you about your
little loop and the general take about the loop, you would be wrong.

I have never seen something like this being ran into the ground and hard at
that.

May 16 '07 #53

Please do me a favor and go lay down, in your crib. I am not here to explain
anything to you. I am not here to hold your hand. I got better things to do.
May 16 '07 #54
Maybe, what I should have said out of the gate was *that your coding was
horrible enough with messing with the idx that the approach could lead to a
blow up with something else you might be trying to do in the future when
jacking around with an idx like that in a loop*.
Yes, that would explained what you meant. And I totally agree. As
does everyone else here.
Really, that's what stood out to me (the red flag) was that idx manipulation
Yes, it's certainly ugly.
It was not until later with ones that needed to post slobbering at the mouth
about the horrible little loop and how it wasn't going to blow up, that this
thread spun out of control, along with the .Net buggy nonsense as well being
thrown in my face.
You said the loop would blow up, and it wouldn't. The only way it
possibly could is if .NET was buggy. Since you made a mis-statement,
which you now admit, everything you said implied this.

So, how could you possibly expect anything but the reaction that
occurred?

Now we know you meant otherwise.
I mis-spoke about the intend of my statement and the blow up condition,
which I should have clarified my statement right out of the gate
Yes, you should have. Especially when people asked. Now that you
have, everything is ok

Well, except you are still complaining that people were calling you on
your mis-statement, but just as we now understand your point now that
you clarified it, why can't you understand why people misunderstood
you, especially since you know you made a mis-statement?

Zytan

May 21 '07 #55
It's not read.

Yeah, go ahead and keep the thread alive. You'll be posting to year 2008. It
looks like.

May 21 '07 #56
T
http://www.arnold.com/

Is this your website, Mr. Arnold?

On May 22, 3:33 am, "Mr. Arnold" <MR. Arn...@Arnold.comwrote:
It's not read.

Yeah, go ahead and keep the thread alive. You'll be posting to year 2008. It
looks like.
May 22 '07 #57

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

Similar topics

14
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for...
9
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private...
7
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
35
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
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,...

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.