生活随笔
收集整理的這篇文章主要介紹了
List的Clear方法与RemoveAll方法用法小结
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
示例代碼
[csharp] view plain copy
using?System; using?System.Collections.Generic; namespace?ListClearExp{????class?Program????{????????static?void?Main(string[]?args)????????{????????????List<int>?intList?=?new?List<int>();????????????Console.WriteLine("1.?初始化列表intList:");????????????Console.WriteLine("intList.Capacity="+intList.Capacity);????????????Console.WriteLine("intList.Count="?+?intList.Count+"\n"); ????????????Console.WriteLine("2.?向intList列表添加元素:");????????????for?(int?i?=?1;?i?<=?5;?i++)????????????{????????????????intList.Add(i);????????????????Console.WriteLine("第"+?i?+?"個(gè)元素為:"?+?i);????????????}????????????Console.WriteLine("intList.Capacity="?+?intList.Capacity);????????????Console.WriteLine("intList.Count="?+?intList.Count+"\n"); ????????????Console.WriteLine("3.?對intList列表進(jìn)行Clear操作:");????????????intList.Clear();????????????foreach?(int?i?in?intList)????????????{????????????????Console.WriteLine(i);????????????}????????????Console.WriteLine("intList.Capacity="?+?intList.Capacity);????????????Console.WriteLine("intList.Count="?+?intList.Count?+?"\n"); ????????????Console.WriteLine("4.?重新初始化intList列表并添加元素:");????????????intList?=?new?List<int>();????????????for?(int?i?=?1;?i?<=?5;?i++)????????????{????????????????intList.Add(i);????????????????Console.WriteLine("第"?+?i?+?"個(gè)元素為:"?+?i);????????????}????????????Console.WriteLine("intList.Capacity="?+?intList.Capacity);????????????Console.WriteLine("intList.Count="?+?intList.Count?+?"\n"); ????????????Console.WriteLine("5.?對intList列表進(jìn)行RemoveAll操作:");????????????intList.RemoveAll(it?=>?true);????????????foreach?(int?i?in?intList)????????????{????????????????Console.WriteLine(i);????????????}????????????Console.WriteLine("intList.Capacity="?+?intList.Capacity);????????????Console.WriteLine("intList.Count="?+?intList.Count);????????}????}} 示例代碼具體操作簡介
1.初始化列表intList,輸出其Capacity及Count值;
2.向intList列表添加元素并輸出,同時(shí)輸出其Capacity及Count值;
3.對intList列表進(jìn)行Clear操作,同時(shí)輸出Clear后的列表元素(很明顯,什么也不輸出)及Capacity和Count值;
4.重新初始化intList列表并添加元素(與步驟2增加相同元素),同時(shí)輸出其Capacity及Count值;
5.對intList列表進(jìn)行RemoveAll操作(條件為it?=>?true,相當(dāng)于刪除所有元素),同時(shí)輸出RemoveAll操作后的列表元素(很明顯,什么也不輸出)及其Capacity和Count值。
?
程序運(yùn)行結(jié)果
?
結(jié)果分析
執(zhí)行List的Clear方法和RemoveAll方法,List將清除指定元素,同時(shí)修改Count屬性值,而Capacity屬性值保持不變。
Clear方法和RemoveAll方法的區(qū)別是:Clear方法會清空所有元素,RemoveAll方法會清空滿足指定條件的元素,當(dāng)將條件設(shè)置為恒成立時(shí),也會刪除所有元素。
下面給出的代碼刪除了intList列表中元素為偶數(shù)的項(xiàng)。
[csharp] view plain copy
using?System; using?System.Collections.Generic; namespace?ListClearExp{????class?Program????{????????static?void?Main(string[]?args)????????{????????????List<int>?intList?=?new?List<int>();????????????for?(int?i?=?1;?i?<=?5;?i++)????????????{????????????????intList.Add(i);????????????????Console.WriteLine("第"?+?i?+?"個(gè)元素為:"?+?i);????????????}????????????intList.RemoveAll(it?=>?it?%?2?==?0);????????????foreach?(int?i?in?intList)????????????{????????????????Console.WriteLine(i);????????????}????????}????}} 代碼運(yùn)行結(jié)果如下所示。
總結(jié)
以上是生活随笔為你收集整理的List的Clear方法与RemoveAll方法用法小结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。