我的代码如下:
                           Hashtable ht = new Hashtable();
ht.Add("First", "Hello");
ht.Add("Second", "World");
ht.Add("Third", "!");


现在如果我想把这个Hashtable的第一个数据取出来,要如何做才可以取出来呢?

解决方案 »

  1.   

    Hashtable ht = new Hashtable();
    ht.Add("First", "Hello");
    ht.Add("Second", "World");
    ht.Add("Third", "!");
    foreach (object o in ht.Values)
        Console.WriteLine(o);
      

  2.   

    hashTable是不排序的,否则就不是散列集合了。你可以使用ArayList来保存有序集合。
      

  3.   

    public class Hashtable : IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback, ICloneable
    请注意:IEnumerable接口
    能够循环遍历
    方法如下:
    Hashtable table = new Hashtable();
    table.Add("item1","张三");
    table.Add("item2","李四");
    IDictionaryEnumerator Enumerator=table.GetEnumerator();
    int i=0;
    while(Enumerator.MoveNext())
    {
       Console.WriteLine("Item: {0} Key:{1}",i,Enumerator.Key);
       Console.WriteLine("Item: {0} Value:{1}",i,Enumerator.Value);
       i++;
    }
    foreach(object o in table.Values)
    {
       Console.Write(o.ToString());
    }
    Console.Read();
    但只是循环遍历而已,不能按照位置次序!正如sp1234所说!
    如果你即要实现Hashtable又要实现ArrayList请使用System.Collections.Specialiazed.NameValueCollection!
      

  4.   

    不好意思上面打错了!是:System.Collections.Specialized.NameValueCollection
      

  5.   

    Hashtable ht = new Hashtable();
    ht.Add("First", "Hello");
    ht.Add("Second", "World");
    ht.Add("Third", "!");
    ArrayList al = new ArrayList();
    foreach (object o in ht.Values)
    {
       al.Add(o.ToString);
    }
    现在我想把数组里的第一个元素放到一个Hashtable类型的变量中,
    我用这个方法就会出错;
    Hashtable hst = new Hashtable();
    hst = (Hashtable)al[0];
    想用这样的方法来获取Hashtable里的值,并传给一个Hashtable类型的变量,但是这样会出错!
      

  6.   

    请问各位有没有做过这种,先把多个值存放到Hashtable中,再把Hashtable中的值又找出来,一个一个的赋给Hashtable类型的变量!
      

  7.   

    因为我现在的方法就是从一个方法中,返回一个Hashtable类型的数组,然后再把返回的Hashtable数组的值传给每个Hashtable类型的变量!