C#中的List .RemoveAt()方法以及示例
C#List<T>.RemoveAt()方法
C#List<T>.RemoveAt()方法用于从列表中删除给定的项目。
语法:
void List<T>.RemoveAt(int index);
参数:接受索引(从0开始)。
返回值:不返回任何内容–返回的类型为void
示例
int list declaration:
List<int> a = new List<int>();
adding elements:
a.Add(10);
a.Add(20);
a.Add(30);
a.Add(40);
a.Add(50);
removing elements
a.RemoveAt(0);
a.RemoveAt(3);
Output:
20 30 40C#使用List<T>.RemoveAt()方法从列表中删除项目的示例
using System;
using System.Text;
using System.Collections.Generic;
namespace Test
{
class Program
{
static void printList(List<int> lst)
{
//打印元素
foreach (int item in lst)
{
Console.Write(item + " ");
}
Console.WriteLine();
}
static void Main(string[] args)
{
//整数列表
List<int> a = new List<int>();
//添加元素
a.Add(10);
a.Add(20);
a.Add(30);
a.Add(40);
a.Add(50);
//打印列表
Console.WriteLine("list elements...");
printList(a);
//删除元素
a.RemoveAt(0);
a.RemoveAt(3);
//删除元素后列出
Console.WriteLine("list elements after removing elements...");
printList(a);
//按ENTER退出
Console.ReadLine();
}
}
}输出结果
list elements... 10 20 30 40 50 list elements after removing elements... 20 30 40
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短