首页 > 开发 > 综合 > 正文

老外编的程序(四)--HashTable的使用示例

2024-07-21 02:20:12
字体:
来源:转载
供稿:网友
using system;
using system.collections;
using system.text;

public class sampleshashtable  {
    public static void main()  {
        // create and initialize a new hashtable.
        hashtable table = new hashtable();
        //student name, grade
        table.add("jay", 100);
        table.add("brian", 87);
        table.add("rajesh", 92);
        table.add("bill", 76);
        table.add("brad", 84);
        table.add("kit", 91);
        table.add("vinaya", 80);
        table.add("lakshan", 87);

        // display the properties and values of the hashtable.    
        console.writeline("count: {0}", table.count );
        printtable( table );

        console.writeline();
        int g = (int) table["jay"];
        console.writeline ("jay's grade is: {0}", g);

        console.writeline();
        printitems ("all names", table.keys);
            
        console.writeline();
        printitems ("all grades", table.values);

    }
    public static void printtable( hashtable mylist )  {
        console.writeline ("{0,-8} {1,-8}", "name","grade");
        console.writeline ("{0,-8} {1,-8}", "----","-----");
        foreach (dictionaryentry e in mylist) {
            console.writeline ("{0,-8} {1,-8}", e.key, e.value);
        }
    }
    public static void printitems(string title, ienumerable    mylist )  {
        console.write ("{0}: ", title);
        stringbuilder sb = new stringbuilder();
        foreach (object o in mylist) {
            sb.appendformat( "{0}, ", o);
        }
        sb.remove (sb.length-2,2);
        console.writeline(sb);
    }
}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表