C# List .Reverse(int index,int count)方法及示例
C#List<T>.Reverse(intindex,intcount)方法
C#List<T>.Reverse(intindex,intcount)方法用于反转列表中的指定元素。
语法:
void List<T>.Reverse(int index, int count);
参数:它接受两个参数:1)索引–我们要反转元素的起始位置;2)计数–索引中元素的总数。
返回值:不返回任何内容–返回的类型为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);
reversing elements:
//从索引1反转3个列表元素
a.Reverse(1,3);
Output:
10 40 30 20 50C#使用List<T>.Reverse(intindex,intcount)方法反转指定列表元素的示例。
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);
if (a.Count > 0)
{
//打印列表
Console.WriteLine("list elements...");
printList(a);
}
else
{
Console.WriteLine("list is empty");
}
//从索引1反转3个列表元素
a.Reverse(1,3);
//反转元素后列出
if (a.Count > 0)
{
Console.WriteLine("list elements after reversing elements...");
printList(a);
}
else
{
Console.WriteLine("list is empty");
}
//按ENTER退出
Console.ReadLine();
}
}
}输出结果
list elements... 10 20 30 40 50 list elements after reversing elements... 10 40 30 20 50
热门推荐
10 爱情贺卡祝福语简短英语
11 给宝贝开学祝福语简短
12 端午发给客户祝福语简短
13 元旦的祝福语简短 公司
14 祖国七十华诞简短祝福语
15 学生节祝福语大全简短
16 订婚新发言简短祝福语
17 女生 节日祝福语简短大气
18 周一情人祝福语简短