C# 程序反转数组

csharpserver side programmingprogramming

首先,设置原始数组 −

int[] arr = { 15, 16, 17, 18 };
// 原始数组
Console.WriteLine("原始数组= ");
foreach (int i in arr) {
   Console.WriteLine(i);
}

现在,使用 Array.reverse() 方法反转数组 −

Array.Reverse(arr);

示例

以下是在 C# 中反转数组的完整代码

using System;
class Demo {
   static void Main() {
      int[] arr = { 15, 16, 17, 18 };
      // 原始数组
      Console.WriteLine("原始数组= ");
      foreach (int i in arr) {
         Console.WriteLine(i);
      }
      // 反转数组
      Array.Reverse(arr);
      Console.WriteLine("反转数组= ");
      foreach (int j in arr) {
         Console.WriteLine(j);
      }
      Console.ReadLine();
   }
}

输出

原始数组=  
15
16
17
18
反转数组=  
18
17
16
15

相关文章