List .Reverse()方法以及C#中的示例
C#List<T>。Reverse()方法
列表<T>。Reverse()方法用于反转所有列表元素。
语法:
void List<T>.Reverse();
参数:不接受任何内容。
返回值:不返回任何内容–返回的类型为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:
a.Reverse();
Output:
50 40 30 20 10C#使用List<T>反转列表元素的示例。Reverse()方法
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");
}
//反向列表元素
a.Reverse();
//反转元素后列出
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... 50 40 30 20 10
热门推荐
10 古风婚礼祝福语唯美简短
11 科研文案祝福语简短励志
12 祝福语同事离职英文简短
13 侄儿生日祝福语简短独特
14 诗意新婚祝福语高级简短
15 朋友搬迁新房祝福语简短
16 牛年新年祝福语大全简短
17 给儿子生日祝福语 简短
18 生日祝福语简短而有深意