加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

js模拟hashtable的简单实例

发布时间:2020-12-14 23:42:05 所属栏目:资源 来源:网络整理
导读:复制代码 代码如下: function Hashtable()//自定义hashtable { this._hash = new Object(); this.add = function(key,value) { if (typeof (key) != "undefined") { if (this.contains(key) == false) { this._hash[key] = typeof (value) == "undefined" ?

复制代码 代码如下:

function Hashtable()//自定义hashtable
{
    this._hash = new Object();
    this.add = function(key,value) {
        if (typeof (key) != "undefined") {
            if (this.contains(key) == false) {
                this._hash[key] = typeof (value) == "undefined" ? null : 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) { return this._hash[key]; }
    this.contains = function(key) { return typeof (this._hash[key]) != "undefined"; }
    this.clear = function() { for (var k in this._hash) { delete this._hash[k]; } }
}

复制代码 代码如下:

// js哈希表
function HashTable() {

    this.ObjArr = {};

    this.Count = 0;

    //添加
    this.Add = function(key,value) {
        if (this.ObjArr.hasOwnProperty(key)) {
            return false; //如果键已经存在,不添加
        }
        else {
            this.ObjArr[key] = value;
            this.Count++;
            return true;
        }
    }

    //是否包含某项
    this.Contains = function(key) {
        return this.ObjArr.hasOwnProperty(key);
    }

    //取某一项 其实等价于this.ObjArr[key]
    this.GetValue = function(key) {
        if (this.Contains(key)) {
            return this.ObjArr[key];
        }
        else {
            throw Error("Hashtable not cotains the key: " + String(key)); //脚本错误
            //return;
        }
    }

    //移除
    this.Remove = function(key) {
        if (this.Contains(key)) {
            delete this.ObjArr[key];
            this.Count--;
        }
    }

    //清空
    this.Clear = function() {
        this.ObjArr = {}; this.Count = 0;
    }
}

测试代码:
//员工
function employee(id,userName) {
    this.id = id;
    this.userName = userName;
}

function test() {

    var ht = new HashTable();
    var tmpEmployee = null;
    for (var i = 1; i < 6; i++) {
        tmpEmployee = new employee(i,"Employee_" + i);
        ht.Add(i,tmpEmployee);
    }
    for (var i = 1; i <= ht.Count; i++) {
        alert(ht.GetValue(i).userName); //其实等价于ht.ObjArr[i].userName
        //alert(ht.ObjArr[i].userName);
    }
    ht.Remove(1);
    alert(ht.Contains(1)); //false
    alert(ht.Contains(2)); //true
    //alert(ht.GetValue(1)); //异常
    var result = ht.GetValue(2);
    if (result != null) {
        alert("Employee Id:" + result.id + ";UserName:" + result.userName);
    }
    ht.Add(2,"这一个key已经存在!"); //Add无效
    //ht.Clear(); //清空
    alert(ht.Count);

}

您可能感兴趣的文章:

  • js中哈希表的几种用法总结
  • javascript 哈希表(hashtable)的简单实现
  • JavaScript中实现键值对应的字典与哈希表结构的示例
  • js实现HashTable(哈希表)的实例分析
  • javascript实现获取字符串hash值
  • 浅谈js多维数组和hash数组定义和使用
  • javascript hashtable实现代码
  • js数组去重的hash方法
  • js实现hashtable的赋值、取值、遍历操作实例详解
  • JS模拟实现哈希表及应用详解

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读