C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet952/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   948   949   950   951   952   953   954   955   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

DictionaryEntry

structure, but you will usually access the keys and values individually using the methods 

and properties defined by 

SortedList

.

The following program demonstrates 



SortedList

. It reworks and expands the 



Hashtable

demonstration program from the previous section, substituting 



SortedList

. When you 

examine the output, you will see that the 

SortedList

 version is sorted by key.

// Demonstrate a SortedList.

using System;

using System.Collections;

Method


Description

public vir tual bool ContainsKey(object 



k

)

Returns true if 



k

 is a key in the invoking 

SortedList

.

Returns false other wise.



public vir tual bool ContainsValue(object 

v

)

Returns true if 



v

 is a value in the invoking 

SortedList

.

Returns false other wise.



public vir tual object GetByIndex(int 

idx

)

Returns the value at the index specified by 



idx.

public vir tual IDictionar yEnumerator

    GetEnumerator( )

Returns an 

IDictionaryEnumerator

 for the invoking 

SortedList

.

public vir tual object GetKey(int 



idx

)

Returns the value of the key at the index specified by 



idx.

public vir tual IList GetKeyList( )

Returns an 

IList


 collection of the keys in the invoking 

SortedList

.

public vir tual IList GetValueList( )



Returns an 

IList


 collection of the values in the invoking 

SortedList

.

public vir tual int IndexOfKey(object 



k

)

Returns the index of the key specified by 



k.

 Returns –1 

if the key is not in the list.

public vir tual int IndexOfValue(object 



v

)

Returns the index of the first occurrence of the value 



specified by 

v.

 Returns –1 if the value is not in the list.

public vir tual void

     SetByIndex(int 



idx

, object 



v

)

Sets the value at the index specified by 



idx

 to the value 

passed in 

v.

public static Sor tedList

    Synchronized(Sor tedList 

sl

)

Returns a synchronized version of the 



SortedList

passed in 



sl.

public vir tual void TrimToSize( )

Sets 

Capacity


 to 

Count


.

T

ABLE



 24-6 

Several Commonly Used Methods Defi ned by 

SortedList

www.freepdf-books.com




766

 

P a r t   I I :  



E x p l o r i n g   t h e   C #   L i b r a r y

class SLDemo {

  static void Main() {

    // Create a sorted SortedList.

    SortedList sl = new SortedList();

    // Add elements to the table.

    sl.Add("house", "Dwelling");

    sl.Add("car", "Means of transport");

    sl.Add("book", "Collection of printed words");

    sl.Add("apple", "Edible fruit");

    // Can also add by using the indexer.

    sl["tractor"] = "Farm implement";

    // Get a collection of the keys.

    ICollection c = sl.Keys;

    // Use the keys to obtain the values.

    Console.WriteLine("Contents of list via indexer.");

    foreach(string str in c)

      Console.WriteLine(str + ": " + sl[str]);

    Console.WriteLine();

    // Display list using integer indexes.

    Console.WriteLine("Contents by integer indexes.");

    for(int i=0; i < sl.Count; i++)

      Console.WriteLine(sl.GetByIndex(i));

    Console.WriteLine();

    // Show integer indexes of entries.

    Console.WriteLine("Integer indexes of entries.");

    foreach(string str in c)

      Console.WriteLine(str + ": " + sl.IndexOfKey(str));

  }

}

The output is shown here:



Contents of list via indexer.

apple: Edible fruit

book: Collection of printed words

car: Means of transport

house: Dwelling

tractor: Farm implement

Contents by integer indexes.

Edible fruit

Collection of printed words

Means of transport

Dwelling

Farm implement

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   948   949   950   951   952   953   954   955   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish