Array.Reverse 메서드

정의

1차원 Array 또는 Array의 일부에 있는 요소의 순서를 역순으로 설정합니다.

오버로드

Reverse(Array)

1차원 Array전체에 있는 요소의 시퀀스를 역순으로 설정합니다.

Reverse(Array, Int32, Int32)

1차원 Array에 있는 요소의 하위 집합 시퀀스를 역순으로 설정합니다.

Reverse<T>(T[])

1차원 제네릭 배열에서 요소의 시퀀스를 역순으로 설정합니다.

Reverse<T>(T[], Int32, Int32)

1차원 제네릭 배열 요소에서 하위 집합 시퀀스를 역방향으로 정렬합니다.

Reverse(Array)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

1차원 Array전체에 있는 요소의 시퀀스를 역순으로 설정합니다.

public:
 static void Reverse(Array ^ array);
public static void Reverse (Array array);
static member Reverse : Array -> unit
Public Shared Sub Reverse (array As Array)

매개 변수

array
Array

역순으로 바꿀 1차원 Array 입니다.

예외

array이(가) null인 경우

array가 다차원 배열인 경우

예제

다음 코드 예제에서는 에서 값의 정렬을 반대로 하는 방법을 보여 주는 합니다 Array.

using namespace System;
void PrintIndexAndValues( Array^ myArray );
void main()
{
   // Creates and initializes a new Array instance.
   Array^ myArray = Array::CreateInstance( String::typeid, 9 );
   myArray->SetValue( "The", 0 );
   myArray->SetValue( "quick", 1 );
   myArray->SetValue( "brown", 2 );
   myArray->SetValue( "fox", 3 );
   myArray->SetValue( "jumps", 4 );
   myArray->SetValue( "over", 5 );
   myArray->SetValue( "the", 6 );
   myArray->SetValue( "lazy", 7 );
   myArray->SetValue( "dog", 8 );

   // Displays the values of the Array.
   Console::WriteLine(  "The Array instance initially contains the following values:" );
   PrintIndexAndValues( myArray );

   // Reverses the sort of the values of the Array.
   Array::Reverse( myArray );
   
   // Displays the values of the Array.
   Console::WriteLine(  "After reversing:" );
   PrintIndexAndValues( myArray );
}

void PrintIndexAndValues( Array^ myArray )
{
   for ( int i = myArray->GetLowerBound( 0 ); i <= myArray->GetUpperBound( 0 ); i++ )
      Console::WriteLine(  "\t[{0}]:\t{1}", i, myArray->GetValue( i ) );
}

/* 
 This code produces the following output.
 
 The Array instance initially contains the following values:
     [0]:    The
     [1]:    quick
     [2]:    brown
     [3]:    fox
     [4]:    jumps
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
 After reversing:
     [0]:    dog
     [1]:    lazy
     [2]:    the
     [3]:    over
     [4]:    jumps
     [5]:    fox
     [6]:    brown
     [7]:    quick
     [8]:    The
 */
open System

let printIndexAndValues (arr: 'a []) =
    for i = arr.GetLowerBound 0 to arr.GetUpperBound 0 do
        printfn $"\t[{i}]:\t{arr[i]}"

// Creates and initializes a new Array.
let myArray = 
    [| "The"; "quick"; "brown"; "fox"
       "jumps"; "over"; "the"; "lazy"; "dog" |]

// Displays the values of the Array.
printfn "The Array initially contains the following values:" 
printIndexAndValues myArray 

// Reverses the sort of the values of the Array.
Array.Reverse myArray 

// Displays the values of the Array.
printfn "After reversing:"
printIndexAndValues myArray 

(*
 This code produces the following output.

 The Array initially contains the following values:
     [0]:    The
     [1]:    quick
     [2]:    brown
     [3]:    fox
     [4]:    jumps
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
 After reversing:
     [0]:    dog
     [1]:    lazy
     [2]:    the
     [3]:    over
     [4]:    jumps
     [5]:    fox
     [6]:    brown
     [7]:    quick
     [8]:    The
 *)
using System;
public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a new Array.
      Array myArray=Array.CreateInstance( typeof(string), 9 );
      myArray.SetValue( "The", 0 );
      myArray.SetValue( "quick", 1 );
      myArray.SetValue( "brown", 2 );
      myArray.SetValue( "fox", 3 );
      myArray.SetValue( "jumps", 4 );
      myArray.SetValue( "over", 5 );
      myArray.SetValue( "the", 6 );
      myArray.SetValue( "lazy", 7 );
      myArray.SetValue( "dog", 8 );

      // Displays the values of the Array.
      Console.WriteLine( "The Array initially contains the following values:" );
      PrintIndexAndValues( myArray );

      // Reverses the sort of the values of the Array.
      Array.Reverse( myArray );

      // Displays the values of the Array.
      Console.WriteLine( "After reversing:" );
      PrintIndexAndValues( myArray );
   }

   public static void PrintIndexAndValues( Array myArray )  {
      for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
         Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) );
   }
}
/*
This code produces the following output.

The Array initially contains the following values:
    [0]:    The
    [1]:    quick
    [2]:    brown
    [3]:    fox
    [4]:    jumps
    [5]:    over
    [6]:    the
    [7]:    lazy
    [8]:    dog
After reversing:
    [0]:    dog
    [1]:    lazy
    [2]:    the
    [3]:    over
    [4]:    jumps
    [5]:    fox
    [6]:    brown
    [7]:    quick
    [8]:    The
*/
Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Array.
        Dim myArray As Array = Array.CreateInstance(GetType(String), 9)
        myArray.SetValue("The", 0)
        myArray.SetValue("quick", 1)
        myArray.SetValue("brown", 2)
        myArray.SetValue("fox", 3)
        myArray.SetValue("jumps", 4)
        myArray.SetValue("over", 5)
        myArray.SetValue("the", 6)
        myArray.SetValue("lazy", 7)
        myArray.SetValue("dog", 8)
        
        ' Displays the values of the Array.
        Console.WriteLine("The Array initially contains the " _
           + "following values:")
        PrintIndexAndValues(myArray)
        
        ' Reverses the sort of the values of the Array.
        Array.Reverse(myArray)
        
        ' Displays the values of the Array.
        Console.WriteLine("After reversing:")
        PrintIndexAndValues(myArray)
    End Sub
    
    
    
    Public Shared Sub PrintIndexAndValues(myArray As Array)
        Dim i As Integer
        For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
            Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
               + "{1}", i, myArray.GetValue(i))
        Next i
    End Sub
End Class

' This code produces the following output.
' 
' The Array initially contains the following values:
'     [0]:    The
'     [1]:    quick
'     [2]:    brown
'     [3]:    fox
'     [4]:    jumps
'     [5]:    over
'     [6]:    the
'     [7]:    lazy
'     [8]:    dog
' After reversing:
'     [0]:    dog
'     [1]:    lazy
'     [2]:    the
'     [3]:    over
'     [4]:    jumps
'     [5]:    fox
'     [6]:    brown
'     [7]:    quick
'     [8]:    The

설명

이 메서드를 호출한 후 의 요소는 myArray[i]i 배열의 인덱스이고 는 로 이동합니다myArray[j]. 여기서 j 는 와 같습니다(myArray.Length + myArray.GetLowerBound(0)) - (i - myArray.GetLowerBound(0)) - 1.

이 메서드는 O(n) 작업입니다. 여기서 n 는 의 array입니다Length.

F#에서는 Array.rev 함수를 대신 사용할 수 있습니다.

다음 예제와 같이 메서드를 Reverse 사용하여 들쭉날쭉한 배열을 되돌릴 수 있습니다. 현재 문화권의 달력에서 현재 연도의 매월마다 하나의 요소를 사용하여 들쭉날쭉한 배열을 초기화합니다. 각 요소에는 해당 월의 일 수만큼 많은 요소가 있는 배열이 포함됩니다. 이 예제에서는 배열의 내용을 표시하고 메서드를 Reverse 호출한 다음 역방향 배열의 내용을 표시합니다.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      int nMonths = DateTimeFormatInfo.CurrentInfo.Calendar.GetMonthsInYear(DateTime.Now.Year);
      int[][] months = new int[nMonths][];

      // Populate elements with number of days in month.
      for (int ctr = 0; ctr <= months.GetUpperBound(0); ctr++) {
         int daysInMonth = DateTime.DaysInMonth(DateTime.Now.Year, ctr + 1);
         months[ctr] = new int[daysInMonth] ;
         for (int ctr2 = 1; ctr2 <= daysInMonth; ctr2++)
            months[ctr][ctr2 - 1] = ctr2;
      }

      foreach (var month in months) {
         foreach (var day in month)
            Console.Write("{0} ", day);

         Console.WriteLine();
      }
      Console.WriteLine();

      Console.WriteLine("About to reverse array.\n");
      Array.Reverse(months);

      foreach (var month in months) {
         foreach (var day in month)
            Console.Write("{0} ", day);

         Console.WriteLine();
      }
   }
}
// The example displays output similar to the following:
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
//
//    About to reverse array.
//
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
open System
open System.Globalization

let nMonths = DateTimeFormatInfo.CurrentInfo.Calendar.GetMonthsInYear DateTime.Now.Year

let months = Array.init nMonths (fun i -> 
    // Populate elements with number of days in month.
    let daysInMonth = DateTime.DaysInMonth(DateTime.Now.Year, i + 1)
    Array.init daysInMonth (fun i -> i + 1 ) )

for month in months do
    for day in month do
        printf $"{day} "
    printfn ""

printfn "\nAbout to reverse array.\n"
Array.Reverse months

for month in months do
    for day in month do
        printf $"{day} "
    printfn ""

// The example displays output similar to the following:
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 
//    
//    About to reverse array.
//    
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 
//    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim nMonths As Integer = DateTimeFormatInfo.CurrentInfo.Calendar.GetMonthsInYear(Date.Now.Year)
      Dim months()() As Integer = New Integer(nMonths - 1)() {}

      ' Populate elements with number of days in month.
      For ctr As Integer = 0 To months.GetUpperBound(0)
         Dim daysInMonth As Integer = DateTime.DaysInMonth(Date.Now.Year, ctr + 1)
         months(ctr) = New Integer(daysInMonth - 1) {}
         For ctr2 As Integer = 1 To daysInMonth
            months(ctr)(ctr2 - 1) = ctr2
         Next
      Next

      For Each _month In months
         For each _day In _month
            Console.Write("{0} ", _day)
         Next   
         Console.WriteLine()
      Next
      Console.WriteLine()
               
      Console.WriteLine("About to reverse array.")
      Console.WriteLine()
      Array.Reverse(months)

      For Each _month In months
         For each _day In _month
            Console.Write("{0} ", _day)
         Next   
         Console.WriteLine()
      Next
   End Sub
End Module
' The example displays output similar to the following:
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
'    
'    About to reverse array.
'    
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
'    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

적용 대상

Reverse(Array, Int32, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

1차원 Array에 있는 요소의 하위 집합 시퀀스를 역순으로 설정합니다.

public:
 static void Reverse(Array ^ array, int index, int length);
public static void Reverse (Array array, int index, int length);
static member Reverse : Array * int * int -> unit
Public Shared Sub Reverse (array As Array, index As Integer, length As Integer)

매개 변수

array
Array

역순으로 바꿀 1차원 Array 입니다.

index
Int32

순서를 바꿀 섹션의 시작 인덱스입니다.

length
Int32

순서를 바꿀 섹션에 있는 요소 수입니다.

예외

array이(가) null인 경우

array가 다차원 배열인 경우

indexarray의 하한값보다 작습니다.

또는

length가 0보다 작은 경우

indexlengtharray의 올바른 범위를 지정하지 않습니다.

예제

다음 코드 예제에서는 의 요소 Array범위에서 값의 정렬을 반대로 하는 방법을 보여 주는 합니다.

using namespace System;
void PrintIndexAndValues( Array^ myArray );
void main()
{
   // Creates and initializes a new Array instance.
   Array^ myArray = Array::CreateInstance( String::typeid, 9 );
   myArray->SetValue( "The", 0 );
   myArray->SetValue( "QUICK", 1 );
   myArray->SetValue( "BROWN", 2 );
   myArray->SetValue( "FOX", 3 );
   myArray->SetValue( "jumps", 4 );
   myArray->SetValue( "over", 5 );
   myArray->SetValue( "the", 6 );
   myArray->SetValue( "lazy", 7 );
   myArray->SetValue( "dog", 8 );

   // Displays the values of the Array.
   Console::WriteLine(  "The Array instance initially contains the following values:" );
   PrintIndexAndValues( myArray );

   // Reverses the sort of the values of the Array.
   Array::Reverse( myArray, 1, 3 );

   // Displays the values of the Array.
   Console::WriteLine(  "After reversing:" );
   PrintIndexAndValues( myArray );
}

void PrintIndexAndValues( Array^ myArray )
{
   for ( int i = myArray->GetLowerBound( 0 ); i <= myArray->GetUpperBound( 0 ); i++ )
      Console::WriteLine(  "\t[{0}]:\t{1}", i, myArray->GetValue( i ) );
}

/* 
 This code produces the following output.
 
 The Array instance initially contains the following values:
     [0]:    The
     [1]:    QUICK
     [2]:    BROWN
     [3]:    FOX
     [4]:    jumps
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
 After reversing:
     [0]:    The
     [1]:    FOX
     [2]:    BROWN
     [3]:    QUICK
     [4]:    jumps
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
 */
open System

let printIndexAndValues (arr: 'a []) =
    for i = arr.GetLowerBound 0 to arr.GetUpperBound 0 do
        printfn $"\t[{i}]:\t{arr[i]}"

// Creates and initializes a new Array.
let myArray = 
    [| "The"; "QUICK"; "BROWN"; "FOX"
       "jumps"; "over"; "the"; "lazy"; "dog" |]

// Displays the values of the Array.
printfn "The Array initially contains the following values:" 
printIndexAndValues myArray 

// Reverses the sort of the values of the Array.
Array.Reverse(myArray, 1, 3)

// Displays the values of the Array.
printfn "After reversing:"
printIndexAndValues myArray 

(*
 This code produces the following output.

 The Array initially contains the following values:
     [0]:    The
     [1]:    QUICK
     [2]:    BROWN
     [3]:    FOX
     [4]:    jumps
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
 After reversing:
     [0]:    The
     [1]:    FOX
     [2]:    BROWN
     [3]:    QUICK
     [4]:    jumps
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
 *)
using System;
public class SamplesArray1  {

   public static void Main()  {

      // Creates and initializes a new Array.
      Array myArray=Array.CreateInstance( typeof(string), 9 );
      myArray.SetValue( "The", 0 );
      myArray.SetValue( "QUICK", 1 );
      myArray.SetValue( "BROWN", 2 );
      myArray.SetValue( "FOX", 3 );
      myArray.SetValue( "jumps", 4 );
      myArray.SetValue( "over", 5 );
      myArray.SetValue( "the", 6 );
      myArray.SetValue( "lazy", 7 );
      myArray.SetValue( "dog", 8 );

      // Displays the values of the Array.
      Console.WriteLine( "The Array initially contains the following values:" );
      PrintIndexAndValues( myArray );

      // Reverses the sort of the values of the Array.
      Array.Reverse( myArray, 1, 3 );

      // Displays the values of the Array.
      Console.WriteLine( "After reversing:" );
      PrintIndexAndValues( myArray );
   }

   public static void PrintIndexAndValues( Array myArray )  {
      for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
         Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) );
   }
}
/*
This code produces the following output.

The Array initially contains the following values:
    [0]:    The
    [1]:    QUICK
    [2]:    BROWN
    [3]:    FOX
    [4]:    jumps
    [5]:    over
    [6]:    the
    [7]:    lazy
    [8]:    dog
After reversing:
    [0]:    The
    [1]:    FOX
    [2]:    BROWN
    [3]:    QUICK
    [4]:    jumps
    [5]:    over
    [6]:    the
    [7]:    lazy
    [8]:    dog
*/
Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Array.
        Dim myArray As Array = Array.CreateInstance(GetType(String), 9)
        myArray.SetValue("The", 0)
        myArray.SetValue("QUICK", 1)
        myArray.SetValue("BROWN", 2)
        myArray.SetValue("FOX", 3)
        myArray.SetValue("jumps", 4)
        myArray.SetValue("over", 5)
        myArray.SetValue("the", 6)
        myArray.SetValue("lazy", 7)
        myArray.SetValue("dog", 8)
        
        ' Displays the values of the Array.
        Console.WriteLine("The Array initially contains the " _
           + "following values:")
        PrintIndexAndValues(myArray)
        
        ' Reverses the sort of the values of the Array.
        Array.Reverse(myArray, 1, 3)
        
        ' Displays the values of the Array.
        Console.WriteLine("After reversing:")
        PrintIndexAndValues(myArray)
    End Sub    
    
    
    Public Shared Sub PrintIndexAndValues(myArray As Array)
        Dim i As Integer
        For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
            Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
               + "{1}", i, myArray.GetValue(i))
        Next i
    End Sub
End Class

' This code produces the following output.
' 
' The Array initially contains the following values:
'     [0]:    The
'     [1]:    QUICK
'     [2]:    BROWN
'     [3]:    FOX
'     [4]:    jumps
'     [5]:    over
'     [6]:    the
'     [7]:    lazy
'     [8]:    dog
' After reversing:
'     [0]:    The
'     [1]:    FOX
'     [2]:    BROWN
'     [3]:    QUICK
'     [4]:    jumps
'     [5]:    over
'     [6]:    the
'     [7]:    lazy
'     [8]:    dog

설명

이 메서드를 호출한 후 의 요소는 myArray[i]i 배열의 인덱스이고 는 로 이동합니다myArray[j]. 여기서 j 는 와 같습니다(myArray.Length + myArray.GetLowerBound(0)) - (i - myArray.GetLowerBound(0)) - 1.

메서드를 Reverse 사용하여 들쭉날쭉한 배열을 되돌릴 수 있습니다.

이 메서드는 O (n) 작업, 여기서 nlength합니다.

적용 대상

Reverse<T>(T[])

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

1차원 제네릭 배열에서 요소의 시퀀스를 역순으로 설정합니다.

public:
generic <typename T>
 static void Reverse(cli::array <T> ^ array);
public static void Reverse<T> (T[] array);
static member Reverse : 'T[] -> unit
Public Shared Sub Reverse(Of T) (array As T())

형식 매개 변수

T

array의 요소 형식입니다.

매개 변수

array
T[]

역방향으로 정렬할 요소의 1차원 배열입니다.

예외

array이(가) null인 경우

array가 다차원 배열인 경우

적용 대상

Reverse<T>(T[], Int32, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

1차원 제네릭 배열 요소에서 하위 집합 시퀀스를 역방향으로 정렬합니다.

public:
generic <typename T>
 static void Reverse(cli::array <T> ^ array, int index, int length);
public static void Reverse<T> (T[] array, int index, int length);
static member Reverse : 'T[] * int * int -> unit
Public Shared Sub Reverse(Of T) (array As T(), index As Integer, length As Integer)

형식 매개 변수

T

array의 요소 형식입니다.

매개 변수

array
T[]

역방향으로 정렬할 요소의 1차원 배열입니다.

index
Int32

순서를 바꿀 섹션의 시작 인덱스입니다.

length
Int32

순서를 바꿀 섹션에 있는 요소 수입니다.

예외

array이(가) null인 경우

array가 다차원 배열인 경우

indexarray의 하한값보다 작습니다.

또는

length가 0보다 작은 경우

indexlengtharray의 올바른 범위를 지정하지 않습니다.

적용 대상