如何按相反順序列印一維陣列?


首先,宣告並初始化一維陣列。

int[] arr = { 35, 12, 66, 90, 34, 2, 64 };

現在,使用以下方法反轉 −

Array.Reverse(arr);

以下是反轉一維陣列的完整程式碼;

示例

 現場演示

using System;
class Demo {
   static void Main() {

      int[] arr = { 76, 12, 66, 90, 34, 2, 64 };
      // Initial Array
      Console.WriteLine("Original Array= ");

      foreach (int i in arr) {
         Console.WriteLine(i);
      }

      // Reverse Array
      Array.Reverse(arr);
      Console.WriteLine("Reversed Array= ");

      foreach (int j in arr) {
         Console.WriteLine(j);
      }
      Console.ReadLine();
   }
}

輸出

Original Array=
76
12
66
90
34
2
64
Reversed Array=
64
2
34
90
66
12
76

更新日期:2020-6-22

425 瀏覽量

開啟你的事業

完成課程獲得認證

開始
廣告
© . All rights reserved.