Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12397309
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类: C#/.net

2015-02-09 14:51:35

主要实现代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;

  6. namespace AlignForHashtable
  7. {
  8.     class Program
  9.     {
  10.         private static Hashtable mHTable = new Hashtable();

  11.         private static void initHTable()
  12.         {
  13.             mHTable.Clear();
  14.             for (int i = 0; i < 10; i++)
  15.             {
  16.                 mHTable.Add(i, "Value" + i);
  17.             }
  18.         }

  19.         // Batch update all the element value .....
  20.         private static void updateHashtableValue(string newValue)
  21.         {
  22.             Hashtable htable = new Hashtable();
  23.             foreach (DictionaryEntry de in mHTable)
  24.             {
  25.                 string valueStr = newValue;
  26.                 htable.Add(de.Key, valueStr);
  27.             }

  28.             mHTable.Clear();
  29.             mHTable = htable;

  30.             /* Error Usage ... */
  31.             //Hashtable htable = this.mHTable;
  32.             //this.mHTable.Clear();
  33.             //string valueStr = string.Empty;

  34.             //foreach (DictionaryEntry de in htable)
  35.             //{
  36.             // valueStr = newValue;
  37.             // this.mHTable.Add(de.Key, valueStr);
  38.             //}
  39.         }

  40.         static void Main(string[] args)
  41.         {
  42.             
  43.             Console.WriteLine("Before update ..... \n");
  44.             initHTable();

  45.             foreach (DictionaryEntry de in mHTable)
  46.             {
  47.                 Console.WriteLine("Key={0},Value={1}", de.Key, de.Value);
  48.             }

  49.             updateHashtableValue("NBA all stars...");

  50.             Console.WriteLine();
  51.             Console.WriteLine("After update ..... \n");

  52.             foreach (DictionaryEntry de in mHTable)
  53.             {
  54.                 Console.WriteLine("Key={0},Value={1}", de.Key, de.Value);
  55.             }

  56.             Console.ReadLine();
  57.         }

  58.     }
  59. }

image

注意!上面代码中,屏蔽代码的更新方法是错误的。

需要留意!!


阅读(1904) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~