国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

js模擬hashtable的簡單實例

2024-05-06 16:02:18
字體:
來源:轉載
供稿:網友
本篇文章主要是對js模擬hashtable的簡單實例進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助

復制代碼 代碼如下:


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);

}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 翼城县| 交口县| 连云港市| 安阳市| 云南省| 菏泽市| 澎湖县| 镇雄县| 银川市| 前郭尔| 靖远县| 南召县| 东乌| 东阿县| 化州市| 祁东县| 巨鹿县| 苍梧县| 修水县| 武乡县| 夏邑县| 江安县| 军事| 曲沃县| 家居| 宁强县| 右玉县| 阿图什市| 吴川市| 盐城市| 锡林郭勒盟| 张掖市| 麟游县| 新建县| 屏东市| 启东市| 榆林市| 饶阳县| 台安县| 榆林市| 柘城县|