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 祝福语驱散霉运的话简短