SortedList.RemoveAt(Int32) Método

Definición

Quita el elemento en el índice especificado de un objeto SortedList.

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

Parámetros

index
Int32

Índice de base cero del elemento que se va a quitar.

Excepciones

El valor de index está fuera del intervalo de índices válidos para el objeto SortedList.

SortedList es de solo lectura.

o bien SortedList tiene un tamaño fijo.

Ejemplos

En el ejemplo de código siguiente se muestra cómo quitar elementos de un SortedList objeto .

#using <system.dll>

using namespace System;
using namespace System::Collections;
void PrintKeysAndValues( SortedList^ myList )
{
   Console::WriteLine( "\t-KEY-\t-VALUE-" );
   for ( int i = 0; i < myList->Count; i++ )
   {
      Console::WriteLine( "\t{0}:\t{1}", myList->GetKey( i ), myList->GetByIndex( i ) );

   }
   Console::WriteLine();
}

int main()
{
   
   // Creates and initializes a new SortedList.
   SortedList^ mySL = gcnew SortedList;
   mySL->Add( "3c", "dog" );
   mySL->Add( "2c", "over" );
   mySL->Add( "1c", "brown" );
   mySL->Add( "1a", "The" );
   mySL->Add( "1b", "quick" );
   mySL->Add( "3a", "the" );
   mySL->Add( "3b", "lazy" );
   mySL->Add( "2a", "fox" );
   mySL->Add( "2b", "jumps" );
   
   // Displays the SortedList.
   Console::WriteLine( "The SortedList initially contains the following:" );
   PrintKeysAndValues( mySL );
   
   // Removes the element with the key "3b".
   mySL->Remove( "3b" );
   
   // Displays the current state of the SortedList.
   Console::WriteLine( "After removing \"lazy\":" );
   PrintKeysAndValues( mySL );
   
   // Removes the element at index 5.
   mySL->RemoveAt( 5 );
   
   // Displays the current state of the SortedList.
   Console::WriteLine( "After removing the element at index 5:" );
   PrintKeysAndValues( mySL );
}

/*
This code produces the following output.

The SortedList initially contains the following:
        -KEY-   -VALUE-
        1a:     The
        1b:     quick
        1c:     brown
        2a:     fox
        2b:     jumps
        2c:     over
        3a:     the
        3b:     lazy
        3c:     dog

After removing "lazy":
        -KEY-   -VALUE-
        1a:     The
        1b:     quick
        1c:     brown
        2a:     fox
        2b:     jumps
        2c:     over
        3a:     the
        3c:     dog

After removing the element at index 5:
        -KEY-   -VALUE-
        1a:     The
        1b:     quick
        1c:     brown
        2a:     fox
        2b:     jumps
        3a:     the
        3c:     dog
*/
using System;
using System.Collections;
public class SamplesSortedList  {

   public static void Main()  {

      // Creates and initializes a new SortedList.
      SortedList mySL = new SortedList();
      mySL.Add( "3c", "dog" );
      mySL.Add( "2c", "over" );
      mySL.Add( "1c", "brown" );
      mySL.Add( "1a", "The" );
      mySL.Add( "1b", "quick" );
      mySL.Add( "3a", "the" );
      mySL.Add( "3b", "lazy" );
      mySL.Add( "2a", "fox" );
      mySL.Add( "2b", "jumps" );

      // Displays the SortedList.
      Console.WriteLine( "The SortedList initially contains the following:" );
      PrintKeysAndValues( mySL );

      // Removes the element with the key "3b".
      mySL.Remove( "3b" );

      // Displays the current state of the SortedList.
      Console.WriteLine( "After removing \"lazy\":" );
      PrintKeysAndValues( mySL );

      // Removes the element at index 5.
      mySL.RemoveAt( 5 );

      // Displays the current state of the SortedList.
      Console.WriteLine( "After removing the element at index 5:" );
      PrintKeysAndValues( mySL );
   }

   public static void PrintKeysAndValues( SortedList myList )  {
      Console.WriteLine( "\t-KEY-\t-VALUE-" );
      for ( int i = 0; i < myList.Count; i++ )  {
         Console.WriteLine( "\t{0}:\t{1}", myList.GetKey(i), myList.GetByIndex(i) );
      }
      Console.WriteLine();
   }
}
/*
This code produces the following output.

The SortedList initially contains the following:
    -KEY-    -VALUE-
    1a:    The
    1b:    quick
    1c:    brown
    2a:    fox
    2b:    jumps
    2c:    over
    3a:    the
    3b:    lazy
    3c:    dog

After removing "lazy":
    -KEY-    -VALUE-
    1a:    The
    1b:    quick
    1c:    brown
    2a:    fox
    2b:    jumps
    2c:    over
    3a:    the
    3c:    dog

After removing the element at index 5:
    -KEY-    -VALUE-
    1a:    The
    1b:    quick
    1c:    brown
    2a:    fox
    2b:    jumps
    3a:    the
    3c:    dog
*/
Imports System.Collections

Public Class SamplesSortedList    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new SortedList.
        Dim mySL As New SortedList()
        mySL.Add("3c", "dog")
        mySL.Add("2c", "over")
        mySL.Add("1c", "brown")
        mySL.Add("1a", "The")
        mySL.Add("1b", "quick")
        mySL.Add("3a", "the")
        mySL.Add("3b", "lazy")
        mySL.Add("2a", "fox")
        mySL.Add("2b", "jumps")
        
        ' Displays the SortedList.
        Console.WriteLine("The SortedList initially contains the following:")
        PrintKeysAndValues(mySL)
        
        ' Removes the element with the key "3b".
        mySL.Remove("3b")
        
        ' Displays the current state of the SortedList.
        Console.WriteLine("After removing ""lazy"":")
        PrintKeysAndValues(mySL)
        
        ' Removes the element at index 5.
        mySL.RemoveAt(5)
        
        ' Displays the current state of the SortedList.
        Console.WriteLine("After removing the element at index 5:")
        PrintKeysAndValues(mySL)
    End Sub
    
    
    Public Shared Sub PrintKeysAndValues(myList As SortedList)
        Console.WriteLine(ControlChars.Tab & "-KEY-" & ControlChars.Tab & _
           "-VALUE-")
        Dim i As Integer
        For i = 0 To myList.Count - 1
            Console.WriteLine(ControlChars.Tab & "{0}:" & ControlChars.Tab & _
               "{1}", myList.GetKey(i), myList.GetByIndex(i))
        Next i
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' The SortedList initially contains the following:
'     -KEY-    -VALUE-
'     1a:    The
'     1b:    quick
'     1c:    brown
'     2a:    fox
'     2b:    jumps
'     2c:    over
'     3a:    the
'     3b:    lazy
'     3c:    dog
' 
' After removing "lazy":
'     -KEY-    -VALUE-
'     1a:    The
'     1b:    quick
'     1c:    brown
'     2a:    fox
'     2b:    jumps
'     2c:    over
'     3a:    the
'     3c:    dog
' 
' After removing the element at index 5:
'     -KEY-    -VALUE-
'     1a:    The
'     1b:    quick
'     1c:    brown
'     2a:    fox
'     2b:    jumps
'     3a:    the
'     3c:    dog

Comentarios

La secuencia de índice se basa en la secuencia de ordenación. Cuando se agrega un elemento, se inserta en SortedList en el criterio de ordenación correcto y la indexación se ajusta en consecuencia. Cuando se quita un elemento, la indexación también se ajusta en consecuencia. Por lo tanto, el índice de un par clave-valor específico puede cambiar a medida que se agregan o quitan elementos del SortedList objeto.

En colecciones de elementos contiguos, como listas, los elementos que van a continuación del elemento eliminado se desplazan hacia arriba para ocupar el espacio libre. Si la colección está indizada, también se actualizan los índices de los elementos que se han movido. Este comportamiento no se aplica a las colecciones cuyos elementos se agrupan conceptualmente en sectores de almacenamiento, como una tabla hash.

Este método es una O(n) operación, donde n es Count.

Se aplica a

Consulte también