• HashTable存储树形数据


    原文:http://www.cnblogs.com/Ivony/archive/2006/04/03/365848.html

    今天在CSDN上遇到一个问题:

    http://community.csdn.net/Expert/topic/4658/4658047.xml?temp=.6713526

    楼主想用树形的数据,本来是可以用XmlDocument的,可这个东西的效率实在是太差,我们推荐他用Hashtable嵌套,后来说写法太复杂。后来就想办法用递归调用来简化语法。

    搞了半天,忽然想起C++里面常常玩的链式表达式的把戏,用下面的代码实现了功能,而且用的时候写法堪称完美。

     1public class TreeNode
     2  
    {
     3
        Hashtable _dictionary;
     4    string _value = ""
    ;
     5

     6    public
     TreeNode()
     7    
    {
     8      _dictionary = new
     Hashtable();
     9    }

    10
    11    public TreeNode this[string
     key]
    12    
    {
    13      get

    14      {
    15        if ( key == null
     )
    16          throw new ArgumentNullException( "key"
     );
    17

    18
            EnsureChildNode( key );
    19

    20        return
     (TreeNode) _dictionary[key];
    21      }

    22      set
    23      {
    24        if ( key == null
     )
    25          throw new ArgumentNullException( "key"
     );
    26

    27
            EnsureChildNode( key );
    28

    29
            ( (TreeNode) _dictionary[key] ).SetValue( value._value );
    30      }

    31    }

    32
    33    private void EnsureChildNode( string
     key )
    34    
    {
    35      if ( !
    _dictionary.ContainsKey( key ) )
    36        _dictionary.Add( key, new
     TreeNode() );
    37    }

    38
    39    public void SetValue( string
     value )
    40    
    {
    41      if ( value == null
     )
    42        throw new ArgumentNullException( "value"
     );
    43

    44

    45      _value =
     value;
    46    }

    47
    48    public override string
     ToString()
    49    
    {
    50      return
     _value;
    51    }

    52
    53    public static implicit operator TreeNode( string
     value )
    54    
    {
    55      TreeNode node = new
     TreeNode();
    56
          node.SetValue( value );
    57      return
     node;
    58    }

    59  }

    60

  • 相关阅读:
    Myeclipse下使用Maven搭建spring boot项目
    Dubbo+Zookeeper视频教程
    dubbo项目实战代码展示
    流程开发Activiti 与SpringMVC整合实例
    交换两个变量的值,不使用第三个变量的四种法方
    数据库主从一致性架构优化4种方法
    数据库读写分离(aop方式完整实现)
    在本地模拟搭建zookeeper集群环境实例
    box-sizing布局
    盒子模型
  • 原文地址:https://www.cnblogs.com/myssh/p/1591993.html
Copyright © 2020-2023  润新知