SortedList.GetByIndex(Int32) 메서드

정의

SortedList 개체의 지정한 인덱스에서 값을 가져옵니다.

public:
 virtual System::Object ^ GetByIndex(int index);
public virtual object GetByIndex (int index);
public virtual object? GetByIndex (int index);
abstract member GetByIndex : int -> obj
override this.GetByIndex : int -> obj
Public Overridable Function GetByIndex (index As Integer) As Object

매개 변수

index
Int32

가져올 값의 0부터 시작하는 인덱스입니다.

반환

Object

SortedList 개체의 지정한 인덱스에 있는 값입니다.

예외

indexSortedList 개체의 유효한 인덱스 범위 밖에 있는 경우

예제

다음 코드 예제에서는 개체의 키 또는 값을 SortedList 하나 또는 모두 가져오는 방법을 보여 있습니다.

#using <system.dll>

using namespace System;
using namespace System::Collections;
int main()
{
   
   // Creates and initializes a new SortedList.
   SortedList^ mySL = gcnew SortedList;
   mySL->Add( 1.3, "fox" );
   mySL->Add( 1.4, "jumps" );
   mySL->Add( 1.5, "over" );
   mySL->Add( 1.2, "brown" );
   mySL->Add( 1.1, "quick" );
   mySL->Add( 1.0, "The" );
   mySL->Add( 1.6, "the" );
   mySL->Add( 1.8, "dog" );
   mySL->Add( 1.7, "lazy" );
   
   // Gets the key and the value based on the index.
   int myIndex = 3;
   Console::WriteLine( "The key   at index {0} is {1}.", myIndex, mySL->GetKey( myIndex ) );
   Console::WriteLine( "The value at index {0} is {1}.", myIndex, mySL->GetByIndex( myIndex ) );
   
   // Gets the list of keys and the list of values.
   IList^ myKeyList = mySL->GetKeyList();
   IList^ myValueList = mySL->GetValueList();
   
   // Prints the keys in the first column and the values in the second column.
   Console::WriteLine( "\t-KEY-\t-VALUE-" );
   for ( int i = 0; i < mySL->Count; i++ )
      Console::WriteLine( "\t{0}\t{1}", myKeyList[ i ], myValueList[ i ] );
}

/*
This code produces the following output.

The key   at index 3 is 1.3.
The value at index 3 is fox.
        -KEY-   -VALUE-
        1       The
        1.1     quick
        1.2     brown
        1.3     fox
        1.4     jumps
        1.5     over
        1.6     the
        1.7     lazy
        1.8     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( 1.3, "fox" );
      mySL.Add( 1.4, "jumps" );
      mySL.Add( 1.5, "over" );
      mySL.Add( 1.2, "brown" );
      mySL.Add( 1.1, "quick" );
      mySL.Add( 1.0, "The" );
      mySL.Add( 1.6, "the" );
      mySL.Add( 1.8, "dog" );
      mySL.Add( 1.7, "lazy" );

      // Gets the key and the value based on the index.
      int myIndex=3;
      Console.WriteLine( "The key   at index {0} is {1}.", myIndex, mySL.GetKey( myIndex ) );
      Console.WriteLine( "The value at index {0} is {1}.", myIndex, mySL.GetByIndex( myIndex ) );

      // Gets the list of keys and the list of values.
      IList myKeyList = mySL.GetKeyList();
      IList myValueList = mySL.GetValueList();

      // Prints the keys in the first column and the values in the second column.
      Console.WriteLine( "\t-KEY-\t-VALUE-" );
      for ( int i = 0; i < mySL.Count; i++ )
         Console.WriteLine( "\t{0}\t{1}", myKeyList[i], myValueList[i] );
   }
}
/*
This code produces the following output.

The key   at index 3 is 1.3.
The value at index 3 is fox.
    -KEY-    -VALUE-
    1    The
    1.1    quick
    1.2    brown
    1.3    fox
    1.4    jumps
    1.5    over
    1.6    the
    1.7    lazy
    1.8    dog
*/
Imports System.Collections

Public Class SamplesSortedList
        
    Public Shared Sub Main()
        
        ' Creates and initializes a new SortedList.
        Dim mySL As New SortedList()
        mySL.Add(1.3, "fox")
        mySL.Add(1.4, "jumps")
        mySL.Add(1.5, "over")
        mySL.Add(1.2, "brown")
        mySL.Add(1.1, "quick")
        mySL.Add(1.0, "The")
        mySL.Add(1.6, "the")
        mySL.Add(1.8, "dog")
        mySL.Add(1.7, "lazy")
        
        ' Gets the key and the value based on the index.
        Dim myIndex As Integer = 3
        Console.WriteLine("The key   at index {0} is {1}.", myIndex, _
           mySL.GetKey(myIndex))
        Console.WriteLine("The value at index {0} is {1}.", myIndex, _
           mySL.GetByIndex(myIndex))
        
        ' Gets the list of keys and the list of values.
        Dim myKeyList As IList = mySL.GetKeyList()
        Dim myValueList As IList = mySL.GetValueList()
        
        ' Prints the keys in the first column and the values in the second column.
        Console.WriteLine(ControlChars.Tab & "-KEY-" & ControlChars.Tab & _
           "-VALUE-")
        Dim i As Integer
        For i = 0 To mySL.Count - 1
            Console.WriteLine(ControlChars.Tab & "{0}" & ControlChars.Tab & _
               "{1}", myKeyList(i), myValueList(i))
        Next i
    End Sub
End Class

' This code produces the following output.
' 
' The key   at index 3 is 1.3.
' The value at index 3 is fox.
'     -KEY-    -VALUE-
'     1    The
'     1.1    quick
'     1.2    brown
'     1.3    fox
'     1.4    jumps
'     1.5    over
'     1.6    the
'     1.7    lazy
'     1.8    dog

설명

인덱스 시퀀스는 정렬 시퀀스를 기반으로 합니다. 요소가 추가되면 올바른 정렬 순서로 SortedList 삽입되고 인덱싱이 적절하게 조정됩니다. 요소가 제거되면 인덱싱도 그에 따라 조정됩니다. 따라서 개체에서 요소가 추가되거나 제거되면 특정 키/값 쌍의 인덱스가 SortedList 변경될 수 있습니다.

이 메서드는 작업입니다 O(1) .

적용 대상

추가 정보