using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace CollectionDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Student s1 = new Student();
            Student s2 = new Student();
            Teacher t1 = new Teacher();
            Teacher t2 = new Teacher();
            s1.Name = "dwyane";
            s1.Id = 1;
            s2.Name = "lebron";
            s2.Id = 2;
            t1.Name = "paul";
            t1.Id = 3;
            t2.Name = "kobe";
            t2.Id = 4;
/*
            ArrayList list = new ArrayList(5)
            list.Add(s1);
            list.Add(s2);
            list.Add(t1);
            list.Add(t2);
*/
            Hashtable map = new Hashtable();
            map.Add(1, s1);
            map.Add(2, s2);
            map.Add(3, t1);
            map.Add(4, t2);
            foreach (DictionaryEntry o in map)
            {                Console.ReadLine();
            }
        }
    }
}
怎么遍历输出名字和学号?

解决方案 »

  1.   

    System.Collections.IDictionaryEnumerator enumerator = map.GetEnumerator(); 
    while (enumerator.MoveNext())
    {
        //Console.WriteLine(enumerator.Key);         // Hashtable关健字
        //Console.WriteLine(enumerator.Value);      // Hashtable值其实是一个Teacher对象
        Teacher t = (Teacher)enumerator.Value;
        Console.WriteLine(t.Name); 
        Console.WriteLine(t.Id); 
        Console.WriteLine("------"); 
    }
      

  2.   

    大致思路如下,具体靠自己美观了using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    using ConsoleApplication1;namespace CollectionDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                Student s1 = new Student();
                Student s2 = new Student();
                Teacher t1 = new Teacher();
                Teacher t2 = new Teacher();
                s1.Name = "dwyane";
                s1.Id = 1;
                s2.Name = "lebron";
                s2.Id = 2;
                t1.Name = "paul";
                t1.Id = 3;
                t2.Name = "kobe";
                t2.Id = 4;
                /*
                            ArrayList list = new ArrayList(5)
                            list.Add(s1);
                            list.Add(s2);
                            list.Add(t1);
                            list.Add(t2);
                */
                Hashtable map = new Hashtable();
                map.Add(1, s1);
                map.Add(2, s2);
                map.Add(3, t1);
                map.Add(4, t2);            foreach (DictionaryEntry o in map)
                {
                    if(o.Value is Teacher)
                    {   
                        var teacher = o.Value as Teacher;
                        Console.WriteLine("{0}, {1}", teacher.Id, teacher.Name);
                    }                if(o.Value is Student)
                    {
                        var student = o.Value as Student;
                        Console.WriteLine("{0}, {1}", student.Id, student.Name);
                    }
                }            Console.ReadLine();
            }
        }
    }
      

  3.   


    为什么输出的顺序是倒过来的?
    看你自己的o.Value是什么了呗
      

  4.   


    http://bbs.csdn.net/topics/30041530HashTable是无序的,如果你真在意顺序,直接用List吧
    用之前转成Object,再加些判断逻辑或者按照下面的方法
    http://hi.baidu.com/cool_bye/item/b81a0d1bb6ce45fb65eabfdd
      

  5.   


    把(Key, 对象)加到HashTabel;
    变量1, 变量2;
    遍历HashTable中的Key
    {
      变量1 = Key对应的对象.学号;
      变量2 = Key对应的对象.姓名;
    }
      

  6.   

    变量改为数组哈!
    数组.Length = HashTable.Count;