일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- python
- dataframe
- 배열
- Shader
- boost
- pandas
- pi
- C++
- with
- List
- 쉘
- autovacuum
- if
- Unity
- FIND
- ngui
- sqlite
- DROP
- awk
- sql
- null
- c++11
- ubuntu
- conda
- SSH
- Eclipse
- postgresql
- SVN
- expect
- mysql
Archives
- Today
- Total
その先にあるもの…
Loop 문에서 list요소 삭제하기 본문
인덱스를 0으로 시작하여 삭제를 진행하면 리스트 중간 요소가 삭제되어 foreeach문에서는 에러가 발생
for문을 사용 하더라도 한 요소를 건너띄는 상황이 생길 수 있다.
방법 1
List< Class > m_List;
for( int i = m_List.Count-1 ; m_List.Count >= 0 ; i++ )
{
if( true )
m_List.Remove( object );
}
방법 2
var list = new List<int>( Enumerable.Range(1, 10) ); <-- insert 1 ~ 10
Console.WriteLine("Before:")
list.ForEach( i => Console.WriteLine(i) );
list.RemoveAll( i => i > 5 );
Console.WriteLine("After:");
list.ForEach( i => Console.WriteLine(i));
참조 : http://stackoverflow.com/questions/1582285/how-to-remove-elements-from-a-generic-list-while-iterating-over-it
'프로그래밍 > C#' 카테고리의 다른 글
ConditionalAttribute (0) | 2015.08.04 |
---|---|
char to int (0) | 2014.12.04 |
클래스가 소멸할 때 멤버변수가 클래스인 것은 null이 되는지 궁금 (0) | 2014.09.19 |
Dictionary value 변경 (0) | 2014.09.01 |
WeakReference (0) | 2014.06.03 |
Comments