Share via


SortedList<TKey,TValue>.Add(TKey, TValue) Metode

Definisi

Menambahkan elemen dengan kunci dan nilai yang ditentukan ke SortedList<TKey,TValue>dalam .

public:
 virtual void Add(TKey key, TValue value);
public void Add (TKey key, TValue value);
abstract member Add : 'Key * 'Value -> unit
override this.Add : 'Key * 'Value -> unit
Public Sub Add (key As TKey, value As TValue)

Parameter

key
TKey

Kunci elemen yang akan ditambahkan.

value
TValue

Nilai elemen yang akan ditambahkan. Nilainya bisa null untuk jenis referensi.

Penerapan

Pengecualian

keyadalah null.

Elemen dengan kunci yang sama sudah ada di SortedList<TKey,TValue>.

Contoh

Contoh kode berikut membuat string kosong SortedList<TKey,TValue> dengan kunci string dan menggunakan Add metode untuk menambahkan beberapa elemen. Contoh menunjukkan bahwa Add metode melemparkan ArgumentException saat mencoba menambahkan kunci duplikat.

Contoh kode ini adalah bagian dari contoh yang lebih besar yang disediakan untuk SortedList<TKey,TValue> kelas .

// Create a new sorted list of strings, with string
// keys.
SortedList<String^, String^>^ openWith =
    gcnew SortedList<String^, String^>();

// Add some elements to the list. There are no 
// duplicate keys, but some of the values are duplicates.
openWith->Add("txt", "notepad.exe");
openWith->Add("bmp", "paint.exe");
openWith->Add("dib", "paint.exe");
openWith->Add("rtf", "wordpad.exe");

// The Add method throws an exception if the new key is
// already in the list.
try
{
    openWith->Add("txt", "winword.exe");
}
catch (ArgumentException^)
{
    Console::WriteLine("An element with Key = \"txt\" already exists.");
}
// Create a new sorted list of strings, with string
// keys.
SortedList<string, string> openWith =
    new SortedList<string, string>();

// Add some elements to the list. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");

// The Add method throws an exception if the new key is
// already in the list.
try
{
    openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
    Console.WriteLine("An element with Key = \"txt\" already exists.");
}
' Create a new sorted list of strings, with string 
' keys. 
Dim openWith As New SortedList(Of String, String)

' Add some elements to the list. There are no 
' duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")

' The Add method throws an exception if the new key is 
' already in the list.
Try
    openWith.Add("txt", "winword.exe")
Catch 
    Console.WriteLine("An element with Key = ""txt"" already exists.")
End Try
// Create a new sorted list of strings, with string
// keys.
let openWith = SortedList<string, string>()

// Add some elements to the list. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")

// The Add method throws an exception if the new key is
// already in the list.
try
    openWith.Add("txt", "winword.exe");
with
    | :? ArgumentException ->
        printfn "An element with Key = \"txt\" already exists."

Keterangan

Kunci tidak boleh null, tetapi nilai bisa, jika jenis nilai dalam daftar yang diurutkan, TValue, adalah jenis referensi.

Anda juga dapat menggunakan Item[] properti untuk menambahkan elemen baru dengan mengatur nilai kunci yang tidak ada di SortedList<TKey,TValue>; misalnya, myCollection["myNonexistentKey"] = myValue. Namun, jika kunci yang ditentukan sudah ada di SortedList<TKey,TValue>, pengaturan Item[] properti akan menimpa nilai lama. Sebaliknya, Add metode tidak memodifikasi elemen yang ada.

Jika Count sudah sama Capacitydengan , kapasitas SortedList<TKey,TValue> ditingkatkan dengan secara otomatis merealokasi array internal, dan elemen yang ada disalin ke array baru sebelum elemen baru ditambahkan.

Metode ini adalah operasi O(n) untuk data yang tidak diurutkan, di mana n adalah Count. Ini adalah operasi O(log n) jika elemen baru ditambahkan di akhir daftar. Jika penyisipan menyebabkan pengurangan ukuran, operasinya adalah O(n).

Berlaku untuk

Lihat juga