473,398 Members | 2,525 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,398 software developers and data experts.

how to remove a item from System.Collections.Generic.List in a loo

Hi,

Following code does not work. (it will crash.)-- I think the reason is that
I cannot modify list (call remove) in foreach. But I don't know what I can
do.

-----------------------------------------------------------------------
List<stringarstr = new List<string>();
//init data
for (int i = 0; i < 30; ++i)
{
arstr.Add(i.ToString());
}

//go through items one by one
//and remove item start with 1 e.g. 1, 10,11...,100...
foreach (string str in arstr)
{
if (str.Length 0 && str[0] == '1')
{
arstr.Remove(str);
}
}

//print the result
foreach (string str in arstr)
{
Console.WriteLine(str);
}

Best Regards,
Michael zhang
Jun 27 '07 #1
4 5517
Hi zhanglr,

Make a new list of removing objects and remove them after the loop (you cannot
modify the list of foreach during its execution).
But, it may be better to make the list of passed objects instead of deleted.

Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com

zHi,
z>
zFollowing code does not work. (it will crash.)-- I think the reason
zis that I cannot modify list (call remove) in foreach. But I don't
zknow what I can do.
z>
z---------------------------------------------------------------------
z--
zList<stringarstr = new List<string>();
z//init data
zfor (int i = 0; i < 30; ++i)
z{
zarstr.Add(i.ToString());
z}
z//go through items one by one
z//and remove item start with 1 e.g. 1, 10,11...,100...
zforeach (string str in arstr)
z{
zif (str.Length 0 && str[0] == '1')
z{
zarstr.Remove(str);
z}
z}
z//print the result
zforeach (string str in arstr)
z{
zConsole.WriteLine(str);
z}
zBest Regards,
zMichael zhang
Jun 27 '07 #2
Hi,

First of all, thank you for your reply.

I just wonder whether there is any better way. E.g. in C++ we are able to
call std::remove_if or list::erase (the return value is iterator so I am able
to continue.) I think maybe MS has provided the samilar functions. I just
haven't found it yet.

At the end, thanks for your reply again.

Best Regards
Michael zhang

"Alex Meleta" wrote:
Hi zhanglr,

Make a new list of removing objects and remove them after the loop (you cannot
modify the list of foreach during its execution).
But, it may be better to make the list of passed objects instead of deleted.

Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com

zHi,
z>
zFollowing code does not work. (it will crash.)-- I think the reason
zis that I cannot modify list (call remove) in foreach. But I don't
zknow what I can do.
z>
z---------------------------------------------------------------------
z--
zList<stringarstr = new List<string>();
z//init data
zfor (int i = 0; i < 30; ++i)
z{
zarstr.Add(i.ToString());
z}
z//go through items one by one
z//and remove item start with 1 e.g. 1, 10,11...,100...
zforeach (string str in arstr)
z{
zif (str.Length 0 && str[0] == '1')
z{
zarstr.Remove(str);
z}
z}
z//print the result
zforeach (string str in arstr)
z{
zConsole.WriteLine(str);
z}
zBest Regards,
zMichael zhang
Jun 27 '07 #3
Hi zhanglr,

Because you will have two arrays: one as a source (as immutable) and second
is filtered. Safe way.

And yes, MS provides similar functions: you can also use e.g. list.RemoveAll(delegate(int
value) { return <some_case_to_return_bool>; }) to clear particular values
(see also other .Remove<somefunctions of list)

Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com

zHi,
z>
zFirst of all, thank you for your reply.
z>
zI just wonder whether there is any better way. E.g. in C++ we are
zable to call std::remove_if or list::erase (the return value is
ziterator so I am able to continue.) I think maybe MS has provided the
zsamilar functions. I just haven't found it yet.
z>
zAt the end, thanks for your reply again.
z>
zBest Regards
zMichael zhang
z"Alex Meleta" wrote:
z>
>Hi zhanglr,

Make a new list of removing objects and remove them after the loop
(you cannot modify the list of foreach during its execution). But, it
may be better to make the list of passed objects instead of deleted.

Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com
zHi,
z>
zFollowing code does not work. (it will crash.)-- I think the
reason
zis that I cannot modify list (call remove) in foreach. But I don't
zknow what I can do.
z>
z>
---------------------------------------------------------------------
z--
zList<stringarstr = new List<string>();
z//init data
zfor (int i = 0; i < 30; ++i)
z{
zarstr.Add(i.ToString());
z}
z//go through items one by one
z//and remove item start with 1 e.g. 1, 10,11...,100...
zforeach (string str in arstr)
z{
zif (str.Length 0 && str[0] == '1')
z{
zarstr.Remove(str);
z}
z}
z//print the result
zforeach (string str in arstr)
z{
zConsole.WriteLine(str);
z}
zBest Regards,
zMichael zhang

Jun 27 '07 #4
Hi,

Thank you so much.

Best Regards,
Michael zhang

"Alex Meleta" wrote:
Hi zhanglr,

Because you will have two arrays: one as a source (as immutable) and second
is filtered. Safe way.

And yes, MS provides similar functions: you can also use e.g. list.RemoveAll(delegate(int
value) { return <some_case_to_return_bool>; }) to clear particular values
(see also other .Remove<somefunctions of list)

Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com

zHi,
z>
zFirst of all, thank you for your reply.
z>
zI just wonder whether there is any better way. E.g. in C++ we are
zable to call std::remove_if or list::erase (the return value is
ziterator so I am able to continue.) I think maybe MS has provided the
zsamilar functions. I just haven't found it yet.
z>
zAt the end, thanks for your reply again.
z>
zBest Regards
zMichael zhang
z"Alex Meleta" wrote:
z>
Hi zhanglr,

Make a new list of removing objects and remove them after the loop
(you cannot modify the list of foreach during its execution). But, it
may be better to make the list of passed objects instead of deleted.

Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com
zHi,
z>
zFollowing code does not work. (it will crash.)-- I think the
reason
zis that I cannot modify list (call remove) in foreach. But I don't
zknow what I can do.
z>
z>
---------------------------------------------------------------------
z--
zList<stringarstr = new List<string>();
z//init data
zfor (int i = 0; i < 30; ++i)
z{
zarstr.Add(i.ToString());
z}
z//go through items one by one
z//and remove item start with 1 e.g. 1, 10,11...,100...
zforeach (string str in arstr)
z{
zif (str.Length 0 && str[0] == '1')
z{
zarstr.Remove(str);
z}
z}
z//print the result
zforeach (string str in arstr)
z{
zConsole.WriteLine(str);
z}
zBest Regards,
zMichael zhang


Jun 27 '07 #5

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

Similar topics

3
by: Abhi | last post by:
In the following hypothetical example I want to build a generic list of unique string items. How should I implement the pred function so that it returns true/false if target string exists in the...
2
by: ljlevend | last post by:
I've noticed that in VS.NET 2.0 Beta 1 that none of the methods in System.Collections.Generic.List are overridable. In my app I currently have over 50 strongly typed ArrayLists that inherit from...
3
by: snesbit | last post by:
I have a structure called SearchAreaListItem. The structure has some properties. The application implements this as a collection.generic.list(of SearchAreaListItem) I load the collection up ...
3
by: Varangian | last post by:
Hello, there I have a problem with regards to System.Collections.Generic.List<T> I need to pass a class with implements an interface - TestClass : IPerson I put this class in a...
2
by: newscorrespondent | last post by:
I have a list declared: public System.Collections.Generic.SortedList<int, System.Collections.Generic.List<MTGTracer ActiveList = new SortedList<int,List<MTGTracer>>(); The key is an integer...
0
by: Susan | last post by:
I have an issue with the System.Collections.Generic.List that I can't find anywhere else... My website suddenly starts giving off System.IndexOutOfRangeException: Index was outside the bounds of...
1
by: Kuldeep | last post by:
Framework: Visual Studio 2005, ASP.NET Programing Language: C#.NET I am using a Generic List Collection to fetch a particular master data from the database. Once collected, I use this Collection...
2
by: Fred Heida | last post by:
Hi, i'm trying to (using managed C++) implment the IEnumerable<Tinterface on my class.. but have a problem with the 2 GetEnumerator method required.... what i have done is... ...
0
by: DR | last post by:
System.Collections.Generic.List<intmyList = new System.Collections.Generic.List<int>(100); Does this preallocate 100 integers? Also, is there any way to preallocate an array of objects all at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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,...
0
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...

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.