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 好听的元旦简短祝福语