ArrayList.Reverse 메서드

정의

ArrayList 또는 그 일부에서 요소의 순서를 반대로 바꿉니다.

오버로드

Reverse()

전체 ArrayList에서 요소의 순서를 반대로 바꿉니다.

Reverse(Int32, Int32)

지정된 범위에서 요소의 순서를 반대로 바꿉니다.

Reverse()

Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
ArrayList.cs

전체 ArrayList에서 요소의 순서를 반대로 바꿉니다.

public:
 virtual void Reverse();
public virtual void Reverse ();
abstract member Reverse : unit -> unit
override this.Reverse : unit -> unit
Public Overridable Sub Reverse ()

예외

ArrayList이 읽기 전용인 경우

예제

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

using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myList );
int main()
{
   
   // Creates and initializes a new ArrayList.
   ArrayList^ myAL = gcnew ArrayList;
   myAL->Add( "The" );
   myAL->Add( "quick" );
   myAL->Add( "brown" );
   myAL->Add( "fox" );
   myAL->Add( "jumps" );
   myAL->Add( "over" );
   myAL->Add( "the" );
   myAL->Add( "lazy" );
   myAL->Add( "dog" );
   
   // Displays the values of the ArrayList.
   Console::WriteLine( "The ArrayList initially contains the following values:" );
   PrintValues( myAL );
   
   // Reverses the sort order of the values of the ArrayList.
   myAL->Reverse();
   
   // Displays the values of the ArrayList.
   Console::WriteLine( "After reversing:" );
   PrintValues( myAL );
}

void PrintValues( IEnumerable^ myList )
{
   IEnumerator^ myEnum = myList->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::WriteLine( "   {0}", obj );
   }

   Console::WriteLine();
}

/* 
 This code produces the following output.
 
 The ArrayList initially contains the following values:
    The
    quick
    brown
    fox
    jumps
    over
    the
    lazy
    dog

 After reversing:
    dog
    lazy
    the
    over
    jumps
    fox
    brown
    quick
    The
 */
using System;
using System.Collections;
public class SamplesArrayList  {

   public static void Main()  {

      // Creates and initializes a new ArrayList.
      ArrayList myAL = new ArrayList();
      myAL.Add( "The" );
      myAL.Add( "quick" );
      myAL.Add( "brown" );
      myAL.Add( "fox" );
      myAL.Add( "jumps" );
      myAL.Add( "over" );
      myAL.Add( "the" );
      myAL.Add( "lazy" );
      myAL.Add( "dog" );

      // Displays the values of the ArrayList.
      Console.WriteLine( "The ArrayList initially contains the following values:" );
      PrintValues( myAL );

      // Reverses the sort order of the values of the ArrayList.
      myAL.Reverse();

      // Displays the values of the ArrayList.
      Console.WriteLine( "After reversing:" );
      PrintValues( myAL );
   }

   public static void PrintValues( IEnumerable myList )  {
      foreach ( Object obj in myList )
         Console.WriteLine( "   {0}", obj );
      Console.WriteLine();
   }
}


/*
This code produces the following output.

The ArrayList initially contains the following values:
   The
   quick
   brown
   fox
   jumps
   over
   the
   lazy
   dog

After reversing:
   dog
   lazy
   the
   over
   jumps
   fox
   brown
   quick
   The
*/
Imports System.Collections

Public Class SamplesArrayList    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new ArrayList.
        Dim myAL As New ArrayList()
        myAL.Add("The")
        myAL.Add("quick")
        myAL.Add("brown")
        myAL.Add("fox")
        myAL.Add("jumps")
        myAL.Add("over")
        myAL.Add("the")
        myAL.Add("lazy")
        myAL.Add("dog")
        
        ' Displays the values of the ArrayList.
        Console.WriteLine("The ArrayList initially contains the " _
           + "following values:")
        PrintValues(myAL)
        
        ' Reverses the sort order of the values of the ArrayList.
        myAL.Reverse()
        
        ' Displays the values of the ArrayList.
        Console.WriteLine("After reversing:")
        PrintValues(myAL)
    End Sub
    
    Public Shared Sub PrintValues(myList As IEnumerable)
        Dim obj As [Object]
        For Each obj In  myList
            Console.WriteLine("   {0}", obj)
        Next obj
        Console.WriteLine()
    End Sub

End Class


' This code produces the following output.
' 
' The ArrayList initially contains the following values:
'    The
'    quick
'    brown
'    fox
'    jumps
'    over
'    the
'    lazy
'    dog
'
' After reversing:
'    dog
'    lazy
'    the
'    over
'    jumps
'    fox
'    brown
'    quick
'    The

설명

이 메서드는 를 사용하여 Array.Reverse 요소의 순서를 반대로 하여 범위 내의 인덱스인 [i]의 요소가 ArrayList [j]로 이동하며 ArrayList 여기서 j는countindex + index + - i - 1입니다.

이 메서드는 작업입니다 O(n) . 여기서 n 은 입니다 Count.

적용 대상

Reverse(Int32, Int32)

Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
ArrayList.cs

지정된 범위에서 요소의 순서를 반대로 바꿉니다.

public:
 virtual void Reverse(int index, int count);
public virtual void Reverse (int index, int count);
abstract member Reverse : int * int -> unit
override this.Reverse : int * int -> unit
Public Overridable Sub Reverse (index As Integer, count As Integer)

매개 변수

index
Int32

순서를 반대로 바꿀 범위의 0부터 시작하는 인덱스입니다.

count
Int32

순서를 반대로 바꿀 범위의 요소 수입니다.

예외

index가 0보다 작은 경우

또는

count가 0보다 작은 경우

indexcountArrayList에서 올바른 요소 범위를 나타내지 않습니다.

ArrayList이 읽기 전용인 경우

예제

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

using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myList );
int main()
{
   
   // Creates and initializes a new ArrayList.
   ArrayList^ myAL = gcnew ArrayList;
   myAL->Add( "The" );
   myAL->Add( "QUICK" );
   myAL->Add( "BROWN" );
   myAL->Add( "FOX" );
   myAL->Add( "jumps" );
   myAL->Add( "over" );
   myAL->Add( "the" );
   myAL->Add( "lazy" );
   myAL->Add( "dog" );
   
   // Displays the values of the ArrayList.
   Console::WriteLine( "The ArrayList initially contains the following values:" );
   PrintValues( myAL );
   
   // Reverses the sort order of the values of the ArrayList.
   myAL->Reverse( 1, 3 );
   
   // Displays the values of the ArrayList.
   Console::WriteLine( "After reversing:" );
   PrintValues( myAL );
}

void PrintValues( IEnumerable^ myList )
{
   IEnumerator^ myEnum = myList->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::WriteLine( "   {0}", obj );
   }

   Console::WriteLine();
}

/* 
 This code produces the following output.
 
 The ArrayList initially contains the following values:
    The
    QUICK
    BROWN
    FOX
    jumps
    over
    the
    lazy
    dog

 After reversing:
    The
    FOX
    BROWN
    QUICK
    jumps
    over
    the
    lazy
    dog

 */
using System;
using System.Collections;
public class SamplesArrayList1  {

   public static void Main()  {

      // Creates and initializes a new ArrayList.
      ArrayList myAL = new ArrayList();
      myAL.Add( "The" );
      myAL.Add( "QUICK" );
      myAL.Add( "BROWN" );
      myAL.Add( "FOX" );
      myAL.Add( "jumps" );
      myAL.Add( "over" );
      myAL.Add( "the" );
      myAL.Add( "lazy" );
      myAL.Add( "dog" );

      // Displays the values of the ArrayList.
      Console.WriteLine( "The ArrayList initially contains the following values:" );
      PrintValues( myAL );

      // Reverses the sort order of the values of the ArrayList.
      myAL.Reverse( 1, 3 );

      // Displays the values of the ArrayList.
      Console.WriteLine( "After reversing:" );
      PrintValues( myAL );
   }

   public static void PrintValues( IEnumerable myList )  {
      foreach ( Object obj in myList )
         Console.WriteLine( "   {0}", obj );
      Console.WriteLine();
   }
}


/*
This code produces the following output.

The ArrayList initially contains the following values:
   The
   QUICK
   BROWN
   FOX
   jumps
   over
   the
   lazy
   dog

After reversing:
   The
   FOX
   BROWN
   QUICK
   jumps
   over
   the
   lazy
   dog

*/
Imports System.Collections

Public Class SamplesArrayList

    Public Shared Sub Main()

        ' Creates and initializes a new ArrayList.
        Dim myAL As New ArrayList()
        myAL.Add("The")
        myAL.Add("QUICK")
        myAL.Add("BROWN")
        myAL.Add("FOX")
        myAL.Add("jumps")
        myAL.Add("over")
        myAL.Add("the")
        myAL.Add("lazy")
        myAL.Add("dog")

        ' Displays the values of the ArrayList.
        Console.WriteLine("The ArrayList initially contains the following values:")
        PrintValues(myAL)

        ' Reverses the sort order of the values of the ArrayList.
        myAL.Reverse(1, 3)

        ' Displays the values of the ArrayList.
        Console.WriteLine("After reversing:")
        PrintValues(myAL)

    End Sub

    Public Shared Sub PrintValues(myList As IEnumerable)
        Dim obj As [Object]
        For Each obj In  myList
            Console.WriteLine("   {0}", obj)
        Next obj
        Console.WriteLine()
    End Sub

End Class


' This code produces the following output.
' 
' The ArrayList initially contains the following values:
'    The
'    QUICK
'    BROWN
'    FOX
'    jumps
'    over
'    the
'    lazy
'    dog
'
' After reversing:
'    The
'    FOX
'    BROWN
'    QUICK
'    jumps
'    over
'    the
'    lazy
'    dog

설명

이 메서드는 를 사용하여 Array.Reverse 요소의 순서를 반대로 하여 범위 내의 인덱스인 [i]의 요소가 ArrayList [j]로 이동하며 ArrayList 여기서 j는countindex + index + - i - 1입니다.

이 메서드는 작업입니다 O(n) . 여기서 n 은 입니다 count.

적용 대상