Csharp/CSharp Tutorial/Data Structure/SortedList — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 18: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.
<source lang="csharp">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)); }
}</source>
Contents by integer indexes. A B C D E
Add value to SortedList and get contents by integer indexes
<source lang="csharp">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)); }
}</source>
Contents by integer indexes. A B C D
Get the index of the element with a key using the IndexOfKey() method
<source lang="csharp">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); }
}</source>
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
<source lang="csharp">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); }
}</source>
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
<source lang="csharp">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); }
}</source>
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
<source lang="csharp">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); } }
}</source>
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
<source lang="csharp">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); } }
}</source>
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
<source lang="csharp">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); } }
}</source>
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
<source lang="csharp">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)); }
}</source>
Integer indexes of entries. a: 0 b: 1 c: 2 d: 3
Use the ContainsValue() method to check if mySortedList contains a value
<source lang="csharp">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"); } }
}</source>
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
<source lang="csharp">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"); }
}</source>
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