Csharp/CSharp Tutorial/Data Structure/SortedList — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 15:31, 26 мая 2010
Содержание
- 1 Add element to SortedList by using the indexer.
- 2 Add value to SortedList and get contents by integer indexes
- 3 Get the index of the element with a key using the IndexOfKey() method
- 4 Get the index of the element with a value using the IndexOfValue() method
- 5 Get the key at index 3 using the GetKey() method
- 6 Get the key list using the GetKeyList() method
- 7 Get the value list using the GetValueList() method
- 8 Get value by key indexer
- 9 Show integer indexes of entries
- 10 Use the ContainsValue() method to check if mySortedList contains a value
- 11 Use the Remove() method to remove a key from SortedList
Add element to SortedList by using the indexer.
using System;
using System.Collections;
class MainClass {
public static void Main() {
SortedList sl = new SortedList();
sl.Add("a", "A");
sl.Add("b", "B");
sl.Add("c", "C");
sl.Add("d", "D");
// add by using the indexer.
sl["e"] = "E";
// Get a collection of the keys.
ICollection c = sl.Keys;
// Display list using integer indexes.
Console.WriteLine("Contents by integer indexes.");
for(int i=0; i<sl.Count; i++)
Console.WriteLine(sl.GetByIndex(i));
}
}
Contents by integer indexes. A B C D E
Add value to SortedList and get contents by integer indexes
using System;
using System.Collections;
class MainClass {
public static void Main() {
SortedList sl = new SortedList();
sl.Add("a", "A");
sl.Add("b", "B");
sl.Add("c", "C");
sl.Add("d", "D");
// Get a collection of the keys.
ICollection c = sl.Keys;
// Display list using integer indexes.
Console.WriteLine("Contents by integer indexes.");
for(int i=0; i<sl.Count; i++)
Console.WriteLine(sl.GetByIndex(i));
}
}
Contents by integer indexes. A B C D
Get the index of the element with a key using the IndexOfKey() method
using System;
using System.Collections;
class MainClass
{
public static void Main()
{
SortedList mySortedList = new SortedList();
mySortedList.Add("NY", "New York");
mySortedList.Add("FL", "Florida");
mySortedList.Add("AL", "Alabama");
mySortedList.Add("WY", "Wyoming");
mySortedList.Add("CA", "California");
foreach (string myKey in mySortedList.Keys)
{
Console.WriteLine("myKey = " + myKey);
}
foreach(string myValue in mySortedList.Values)
{
Console.WriteLine("myValue = " + myValue);
}
int myIndex = mySortedList.IndexOfKey("NY");
Console.WriteLine("The index of NY is " + myIndex);
}
}
myKey = AL myKey = CA myKey = FL myKey = NY myKey = WY myValue = Alabama myValue = California myValue = Florida myValue = New York myValue = Wyoming The index of NY is 3
Get the index of the element with a value using the IndexOfValue() method
using System;
using System.Collections;
class MainClass
{
public static void Main()
{
SortedList mySortedList = new SortedList();
mySortedList.Add("NY", "New York");
mySortedList.Add("FL", "Florida");
mySortedList.Add("AL", "Alabama");
mySortedList.Add("WY", "Wyoming");
mySortedList.Add("CA", "California");
foreach (string myKey in mySortedList.Keys)
{
Console.WriteLine("myKey = " + myKey);
}
foreach(string myValue in mySortedList.Values)
{
Console.WriteLine("myValue = " + myValue);
}
int myIndex = mySortedList.IndexOfValue("New York");
Console.WriteLine("The index of New York is " + myIndex);
}
}
myKey = AL myKey = CA myKey = FL myKey = NY myKey = WY myValue = Alabama myValue = California myValue = Florida myValue = New York myValue = Wyoming The index of New York is 3
Get the key at index 3 using the GetKey() method
using System;
using System.Collections;
class MainClass
{
public static void Main()
{
SortedList mySortedList = new SortedList();
mySortedList.Add("NY", "New York");
mySortedList.Add("FL", "Florida");
mySortedList.Add("AL", "Alabama");
mySortedList.Add("WY", "Wyoming");
mySortedList.Add("CA", "California");
foreach (string myKey in mySortedList.Keys)
{
Console.WriteLine("myKey = " + myKey);
}
foreach(string myValue in mySortedList.Values)
{
Console.WriteLine("myValue = " + myValue);
}
string keyAtIndex3 = (string) mySortedList.GetKey(3);
Console.WriteLine("The key at index 3 is " + keyAtIndex3);
}
}
myKey = AL myKey = CA myKey = FL myKey = NY myKey = WY myValue = Alabama myValue = California myValue = Florida myValue = New York myValue = Wyoming The key at index 3 is NY
Get the key list using the GetKeyList() method
using System;
using System.Collections;
class MainClass
{
public static void Main()
{
SortedList mySortedList = new SortedList();
mySortedList.Add("NY", "New York");
mySortedList.Add("FL", "Florida");
mySortedList.Add("AL", "Alabama");
mySortedList.Add("WY", "Wyoming");
mySortedList.Add("CA", "California");
foreach (string myKey in mySortedList.Keys)
{
Console.WriteLine("myKey = " + myKey);
}
foreach(string myValue in mySortedList.Values)
{
Console.WriteLine("myValue = " + myValue);
}
Console.WriteLine("Getting the key list");
IList myKeyList = mySortedList.GetKeyList();
foreach(string myKey in myKeyList)
{
Console.WriteLine("myKey = " + myKey);
}
}
}
myKey = AL myKey = CA myKey = FL myKey = NY myKey = WY myValue = Alabama myValue = California myValue = Florida myValue = New York myValue = Wyoming Getting the key list myKey = AL myKey = CA myKey = FL myKey = NY myKey = WY
Get the value list using the GetValueList() method
using System;
using System.Collections;
class MainClass
{
public static void Main()
{
SortedList mySortedList = new SortedList();
mySortedList.Add("NY", "New York");
mySortedList.Add("FL", "Florida");
mySortedList.Add("AL", "Alabama");
mySortedList.Add("WY", "Wyoming");
mySortedList.Add("CA", "California");
foreach (string myKey in mySortedList.Keys)
{
Console.WriteLine("myKey = " + myKey);
}
foreach(string myValue in mySortedList.Values)
{
Console.WriteLine("myValue = " + myValue);
}
Console.WriteLine("Getting the value list");
IList myValueList = mySortedList.GetValueList();
foreach(string myValue in myValueList)
{
Console.WriteLine("myValue = " + myValue);
}
}
}
myKey = AL myKey = CA myKey = FL myKey = NY myKey = WY myValue = Alabama myValue = California myValue = Florida myValue = New York myValue = Wyoming Getting the value list myValue = Alabama myValue = California myValue = Florida myValue = New York myValue = Wyoming
Get value by key indexer
using System;
using System.Collections;
class MainClass
{
public static void Main()
{
SortedList mySortedList = new SortedList();
mySortedList.Add("NY", "New York");
mySortedList.Add("FL", "Florida");
mySortedList.Add("AL", "Alabama");
mySortedList.Add("WY", "Wyoming");
mySortedList.Add("CA", "California");
string myState = (string) mySortedList["CA"];
Console.WriteLine("myState = " + myState);
// display the keys for mySortedList using the Keys property
foreach (string myKey in mySortedList.Keys) {
Console.WriteLine("myKey = " + myKey);
}
// display the values for mySortedList using the Values property
foreach(string myValue in mySortedList.Values) {
Console.WriteLine("myValue = " + myValue);
}
}
}
myState = California myKey = AL myKey = CA myKey = FL myKey = NY myKey = WY myValue = Alabama myValue = California myValue = Florida myValue = New York myValue = Wyoming
Show integer indexes of entries
using System;
using System.Collections;
class MainClass {
public static void Main() {
SortedList sl = new SortedList();
sl.Add("a", "1");
sl.Add("b", "2");
sl.Add("c", "3");
sl.Add("d", "4");
Console.WriteLine();
// Show integer indexes of entries.
Console.WriteLine("Integer indexes of entries.");
foreach(string str in sl.Keys)
Console.WriteLine(str + ": " + sl.IndexOfKey(str));
}
}
Integer indexes of entries. a: 0 b: 1 c: 2 d: 3
Use the ContainsValue() method to check if mySortedList contains a value
using System;
using System.Collections;
class MainClass
{
public static void Main()
{
SortedList mySortedList = new SortedList();
mySortedList.Add("NY", "New York");
mySortedList.Add("FL", "Florida");
mySortedList.Add("AL", "Alabama");
mySortedList.Add("WY", "Wyoming");
mySortedList.Add("CA", "California");
foreach (string myKey in mySortedList.Keys)
{
Console.WriteLine("myKey = " + myKey);
}
foreach(string myValue in mySortedList.Values)
{
Console.WriteLine("myValue = " + myValue);
}
if (mySortedList.ContainsValue("Florida"))
{
Console.WriteLine("mySortedList contains the value Florida");
}
}
}
myKey = AL myKey = CA myKey = FL myKey = NY myKey = WY myValue = Alabama myValue = California myValue = Florida myValue = New York myValue = Wyoming mySortedList contains the value Florida
Use the Remove() method to remove a key from SortedList
using System;
using System.Collections;
class MainClass
{
public static void Main()
{
SortedList mySortedList = new SortedList();
mySortedList.Add("NY", "New York");
mySortedList.Add("FL", "Florida");
mySortedList.Add("AL", "Alabama");
mySortedList.Add("WY", "Wyoming");
mySortedList.Add("CA", "California");
foreach (string myKey in mySortedList.Keys)
{
Console.WriteLine("myKey = " + myKey);
}
foreach(string myValue in mySortedList.Values)
{
Console.WriteLine("myValue = " + myValue);
}
Console.WriteLine("Removing FL from mySortedList");
mySortedList.Remove("FL");
}
}
myKey = AL myKey = CA myKey = FL myKey = NY myKey = WY myValue = Alabama myValue = California myValue = Florida myValue = New York myValue = Wyoming Removing FL from mySortedList