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
Do'stlaringiz bilan baham: |