javascript HashTable

//哈希表
function HashTable() {
    //存数据的实体
    this._Hash = new Object();

    //新增键值
    this.Add = function (key, value) {
        if (typeof (key) != "undefined") {
            if (this.Contains(key) == false) {
                this._Hash[key] = value;
                return true;
            }
            else {
                return false;
            }
        }
        else {
            return false;
        }
    }

    //删除键值
    this.Remove = function (key) { delete this._Hash[key]; }

    //数量
    this.Count = function () { var i = 0; for (var k in this._Hash) { i++; } return i; }

    //获取值
    this.Items = function (key) {
        var value = this._Hash[key];
        return typeof (value) == "undefined" ? null : value
    }

    //获取值
    this.GetValue = function (key) {
        return this.Items(key);
    }

    //获取值
    this.Get = function (key) {
        return this.Items(key);
    }

    //赋值
    this.SetValue = function (key, value) {
        return this._Hash[key] = value;
    }

    //是否包含键
    this.Contains = function (key) { return typeof (this._Hash[key]) != "undefined"; }

    //清空哈希表
    this.Clear = function () { for (var k in this._Hash) { delete this._Hash[k]; } }

    //获取所有键
    this.AllKeys = function () { return this._Hash; }
}


 



 新建:  var  ht = new HashTable();

添加: ht.Add("kk","hello");

遍历:

<pre class="prettyprint javascript" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-top: 0px; margin-bottom: 1.5em; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;"><span style="white-space:pre">	</span>for</span></span> (<span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">var</span></span> i = <span style="color: rgb(128, 0, 128);"><span class="number" style="color: rgb(0, 153, 153);">1</span></span>; i < <span style="color: rgb(128, 0, 128);"><span class="number" style="color: rgb(0, 153, 153);">6</span></span>; i++<span style="color: rgb(0, 0, 0);">) { 
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>tmpEmployee </span>= <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">new</span></span> employee(i, <span style="color: rgb(128, 0, 0);"><span class="string" style="color: rgb(221, 17, 68);">"</span></span><span class="string" style="color: rgb(221, 17, 68);"><span style="color: rgb(128, 0, 0);">Employee_</span><span style="color: rgb(128, 0, 0);">"</span></span><span style="color: rgb(128, 0, 0);"></span> +<span style="color: rgb(0, 0, 0);"> i); 
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>ht.Add(i, tmpEmployee); 
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>} 
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span></span><span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">for</span></span> (<span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">var</span></span> i = <span style="color: rgb(128, 0, 128);"><span class="number" style="color: rgb(0, 153, 153);">1</span></span>; i <= ht.Count; i++<span style="color: rgb(0, 0, 0);">) { 
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>alert(ht.GetValue(i).userName); </span><span style="color: rgb(0, 128, 0);"><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">//</span></span><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;"><span style="color: rgb(0, 128, 0);">其实等价于ht.ObjArr[i].userName </span></span><span style="color: rgb(0, 128, 0);">
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span></span><span style="color: rgb(0, 128, 0);"><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">//</span></span><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;"><span style="color: rgb(0, 128, 0);">alert(ht.ObjArr[i].userName); </span></span><span style="color: rgb(0, 128, 0);"></span>
<span style="color: rgb(0, 0, 0);"><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>} </span>

 
for (var i = 1; i <= ht.Count; i++) { 
        alert(ht.GetValue(i)); //其实等价于ht.ObjArr[i].userName 
         
      } 



参考:http://www.tuicool.com/articles/67NVzq


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值