ArrayList.Reverse Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Inverte a ordem dos elementos no ArrayList ou uma parte dele.
Sobrecargas
| Name | Description |
|---|---|
| Reverse() |
Inverte a ordem dos elementos em todo ArrayListo . |
| Reverse(Int32, Int32) |
Inverte a ordem dos elementos no intervalo especificado. |
Reverse()
Inverte a ordem dos elementos em todo ArrayListo .
public:
virtual void Reverse();
public virtual void Reverse();
abstract member Reverse : unit -> unit
override this.Reverse : unit -> unit
Public Overridable Sub Reverse ()
Exceções
É ArrayList só de leitura.
Exemplos
O seguinte exemplo de código mostra como inverter a ordem de ordenação dos valores num ArrayList.
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
Observações
Este método serve Array.Reverse para inverter a ordem dos elementos, de modo que o elemento em ArrayList [i], onde i é qualquer índice dentro do intervalo, se mova para ArrayList [j], onde j écountindex + index + igual a - i - 1.
Este método é uma O(n) operação, onde n é Count.
Aplica-se a
Reverse(Int32, Int32)
Inverte a ordem dos elementos no intervalo especificado.
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)
Parâmetros
- index
- Int32
O índice inicial baseado em zero do intervalo a inverter.
- count
- Int32
O número de elementos no intervalo a inverter.
Exceções
index e count não denotam um intervalo válido de elementos no ArrayList.
É ArrayList só de leitura.
Exemplos
O seguinte exemplo de código mostra como inverter a ordem de ordenação dos valores num intervalo de elementos de um ArrayList.
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
Observações
Este método serve Array.Reverse para inverter a ordem dos elementos, de modo que o elemento em ArrayList [i], onde i é qualquer índice dentro do intervalo, se mova para ArrayList [j], onde j écountindex + index + igual a - i - 1.
Este método é uma O(n) operação, onde n é count.