列表删除(T)方法 习惯于 删除特定对象的第一个引用 从名单上。
null
列表的属性:
- 它与阵列不同。A. 列表可以动态调整大小 但数组不能。
- List类可以接受null作为引用类型的有效值,并且 还允许复制元素 .
- 如果计数等于容量,则通过重新分配内部数组,列表的容量会自动增加。在添加新元素之前,现有元素将被复制到新数组中。
语法:
public bool Remove (T item);
参数:
项目: 要从列表中删除的指定对象。
返回类型: 此方法返回 符合事实的 如果 项目 已成功删除。否则它就会回来 错误的 .
注: 此方法返回 错误的 如果 项目 在列表中找不到。
以下程序说明了如何从列表中删除指定的元素:
例1:
// C# program to remove the specified // element from the List<T> using System; using System.Collections.Generic; class Geeks { // Main Method public static void Main(String[] args) { // Creating a List of integers List< int > firstlist = new List< int >(); // adding elements in firstlist firstlist.Add(1); firstlist.Add(2); firstlist.Add(3); firstlist.Add(4); // Displaying elements of firstlist // by using foreach loop Console.WriteLine( "Before Removing" ); foreach ( int element in firstlist) { Console.WriteLine(element); } // Removing 2 from the firstlist & Displaying // the remaining firstlist elements Console.WriteLine( "After Removing" ); firstlist.Remove(2); foreach ( int element in firstlist) { Console.WriteLine(element); } } } |
输出:
Before Removing 1 2 3 4 After Removing 1 3 4
例2:
// C# program to remove the specified // element from the List<T> using System; using System.Collections.Generic; class Geeks { // Main Method public static void Main(String[] args) { // Creating a List of integers List< int > firstlist = new List< int >(); // adding elements in firstlist firstlist.Add(1); firstlist.Add(2); firstlist.Add(3); firstlist.Add(4); // Adding some duplicate // elements in firstlist firstlist.Add(2); firstlist.Add(4); // Displaying elements of firstlist // by using foreach loop Console.WriteLine( "Before Removing" ); foreach ( int element in firstlist) { Console.WriteLine(element); } // Removing first occurrence of 2 // from the firstlist & Displaying // the remaining firstlist elements Console.WriteLine( "After Removing" ); firstlist.Remove(2); foreach ( int element in firstlist) { Console.WriteLine(element); } } } |
输出:
Before Removing 1 2 3 4 2 4 After Removing 1 3 4 2 4
参考:
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END