📌  相关文章
📜  C#|颠倒整个列表或指定范围中元素的顺序

📅  最后修改于: 2021-05-29 23:36:14             🧑  作者: Mango

List .Reverse方法用于反转List 中元素或其一部分的顺序。 List 的重载列表中有两种方法。反向方法如下:

  • 撤销()
  • 反转(Int32,Int32)

Reverse()方法

此方法用于反转整个List 中元素的顺序。

句法:

public void Reverse ();

下面的程序说明了上面讨论的方法的使用:

范例1:

// C# Program to reverse the order of
// the elements in the entire List
using System;
using System.Collections;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main()
    {
  
        // Creating an List of Integers
        List firstlist = new List();
  
        // Adding elements to List
        for (int i = 1; i <= 5; i++) {
            firstlist.Add(i);
        }
  
        Console.WriteLine("Elements Present in List:");
  
        // Displaying the elements of List
        foreach(int k in firstlist)
        {
            Console.WriteLine(k);
        }
  
        Console.WriteLine(" ");
  
        Console.WriteLine("After Reversing: ");
  
        // using method Reverse()
        firstlist.Reverse();
  
        // Displaying the elements of List
        foreach(int k in firstlist)
        {
            Console.WriteLine(k);
        }
    }
}

输出:

Elements Present in List:
1
2
3
4
5
 
After Reversing: 
5
4
3
2
1

范例2:

// C# Program to reverse the order of
// the elements in the entire List
using System;
using System.Collections;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main()
    {
  
        // Creating an List of Integers
        List firstlist = new List();
  
        // Adding elements to List
        firstlist.Add("Geeks");
        firstlist.Add("C#");
        firstlist.Add("Java");
        firstlist.Add("C++");
  
        Console.WriteLine("Elements Present in List:");
  
        // Displaying the elements of List
        foreach(string k in firstlist)
        {
            Console.WriteLine(k);
        }
  
        Console.WriteLine(" ");
  
        Console.WriteLine("After Reversing: ");
  
        // using method Reverse()
        firstlist.Reverse();
  
        // Displaying the elements of List
        foreach(string k in firstlist)
        {
            Console.WriteLine(k);
        }
    }
}

输出:

Elements Present in List:
Geeks
C#
Java
C++
 
After Reversing: 
C++
Java
C#
Geeks

Reverse(Int32,Int32)方法

此方法用于反转指定范围内元素的顺序。

句法:

public void Reverse (int index, int count);

参数:

例外情况:

  • ArgumentOutOfRangeException:如果索引计数小于零。
  • ArgumentException:如果索引计数未表示List 中元素的有效范围。

下面的程序说明了上面讨论的方法的使用:

范例1:

// C# Program to reverse the order of
// sub range in the List
using System;
using System.Collections;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main()
    {
  
        // Creating an List of Integers
        List firstlist = new List();
  
        // Adding elements to List
        firstlist.Add("1st");
        firstlist.Add("2nd");
        firstlist.Add("3rd");
        firstlist.Add("4th");
        firstlist.Add("5th");
        firstlist.Add("6th");
        firstlist.Add("7th");
  
        Console.WriteLine("Elements Present in List:");
  
        // Displaying the elements of List
        foreach(string k in firstlist)
        {
            Console.WriteLine(k);
        }
  
        Console.WriteLine(" ");
  
        Console.WriteLine("After Reversing: ");
  
        // Reversing the sub-range
        // that starts from index 2
        // i.e 3rd element, and
        // count is 4 elements
        firstlist.Reverse(2, 4);
  
        // Displaying the elements of List
        foreach(string k in firstlist)
        {
            Console.WriteLine(k);
        }
    }
}

输出:

Elements Present in List:
1st
2nd
3rd
4th
5th
6th
7th
 
After Reversing: 
1st
2nd
6th
5th
4th
3rd
7th

范例2:

// C# Program to reverse the order of
// sub range in the List
using System;
using System.Collections;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main()
    {
  
        // Creating an List of Integers
        List firstlist = new List();
  
        // Adding elements to List
        firstlist.Add("1st");
        firstlist.Add("2nd");
        firstlist.Add("3rd");
        firstlist.Add("4th");
        firstlist.Add("5th");
        firstlist.Add("6th");
        firstlist.Add("7th");
  
        Console.WriteLine("Elements Present in List:");
  
        // Displaying the elements of List
        foreach(string k in firstlist)
        {
            Console.WriteLine(k);
        }
  
        Console.WriteLine(" ");
  
        Console.WriteLine("After Reversing: ");
  
        // taking negative index will give error
        firstlist.Reverse(-1, 4);
  
        // Displaying the elements of List
        foreach(string k in firstlist)
        {
            Console.WriteLine(k);
        }
    }
}

RUntimeError:

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.list-1.reverse?view=netframework-4.7.2