Csharp/C Sharp by API/System.Collections/SortedList — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:31, 26 мая 2010

SortedList.Add

  
    using System;
    using System.Collections;
class Class1 {
   static void Main(string[] args) {
      bool ByEmp = false;
      SortedList Emps = new SortedList();
      Emps.Add("500", "A");
      Emps.Add("502", "C");
      Emps.Add("501", "B");
      Emps.Add("503", "D");
      
      for(int k=0; k<Emps.Count; k++) {
        Console.WriteLine("\t{0}\t{1}", Emps.GetKey(k), Emps.GetByIndex(k));
      }
   }
}


SortedList.ContainsValue()

  
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");
    }
  }
}


SortedList.Count

  
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)); 
      
  }
}


SortedList.GetByIndex

  
    using System;
    using System.Collections;
class Class1 {
   static void Main(string[] args) {
      bool ByEmp = false;
      SortedList Emps = new SortedList();
      Emps.Add("500", "A");
      Emps.Add("502", "C");
      Emps.Add("501", "B");
      Emps.Add("503", "D");
      
      for(int k=0; k<Emps.Count; k++) {
        Console.WriteLine("\t{0}\t{1}", Emps.GetKey(k), Emps.GetByIndex(k));
      }
   }
}


SortedList.GetKey

  
    using System;
    using System.Collections;
class Class1 {
   static void Main(string[] args) {
      bool ByEmp = false;
      SortedList Emps = new SortedList();
      Emps.Add("500", "A");
      Emps.Add("502", "C");
      Emps.Add("501", "B");
      Emps.Add("503", "D");
      
      for(int k=0; k<Emps.Count; k++) {
        Console.WriteLine("\t{0}\t{1}", Emps.GetKey(k), Emps.GetByIndex(k));
      }
   }
}


SortedList.GetKeyList

  

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);
    }
  }
}


SortedList.GetValueList

  
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);
    }
  }
}


SortedList.IndexOfKey

  
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);
  }
}


SortedList.Keys

  
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)); 
      
  }
}


SortedList.Remove

  
using System;
using System.Collections;

public class MainClass {
    public static void Main() {
        SortedList list = new SortedList();
        list.Add("G", "B");
        list.Add("Ae", "Sm");
        list.Add("Ay", "G");
        list.Add("Ab", "S");
        list.Add("W", "C");
        list.Remove("Abraxus");
        list.RemoveAt(2);
        String artist = (string)list["Ay"];
        Console.WriteLine(artist);
        String error = (string)list["S"];
        Console.WriteLine(error);
        foreach (DictionaryEntry d in list) {
            Console.WriteLine("{0}, {1}", d.Key, d.Value);
        }
    }
}


SortedList.RemoveAt

  
using System;
using System.Collections;

public class MainClass {
    public static void Main() {
        SortedList list = new SortedList();
        list.Add("G", "B");
        list.Add("Ae", "Sm");
        list.Add("Ay", "G");
        list.Add("Ab", "S");
        list.Add("W", "C");
        list.Remove("Abraxus");
        list.RemoveAt(2);
        String artist = (string)list["Ay"];
        Console.WriteLine(artist);
        String error = (string)list["S"];
        Console.WriteLine(error);
        foreach (DictionaryEntry d in list) {
            Console.WriteLine("{0}, {1}", d.Key, d.Value);
        }
    }
}


SortedList.Values

  

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");
    }
  }
}