大家好,又见面了,我是你们的朋友全栈君。
只是根据数组方式记录顺序 需要其他功能自己改写吧 或者有更好办法可以告诉我
代码片
.
function HashMap() {
//维护映射
this.map = {
};
//维护顺序
this.arrayLink=[]
}
HashMap.prototype = {
put: function (key, value) {
// 向Map中增加元素(key, value)
var numb= this.arrayLink.indexOf(key)
// console.log(numb)
if ( numb==-1) {
this.arrayLink.push(key) ;
numb= this.arrayLink.indexOf(key)
}
// console.log(numb)
// console.log(this.arrayLink)
this.map[numb] = value;
},
get: function (key) {
//获取指定Key的元素值Value,失败返回Null
var numb= this.arrayLink.indexOf(key)
if (this.map.hasOwnProperty(numb)) {
return this.map[numb];
}else {
return null;
}
},
remove: function (key) {
// 删除指定Key的元素,成功返回True,失败返回False
var numb= this.arrayLink.indexOf(key)
this.arrayLink.splice(numb, 1, "-1")
//喊头不含尾
var arrayLinktt=this.arrayLink.slice(0, numb)
var arrayLinktt1=this.arrayLink.slice(numb+1, this.arrayLink.length)
this.arrayLink = [];
this.arrayLink.concat(arrayLinktt)
this.arrayLink.concat(arrayLinktt1)
if (this.map.hasOwnProperty(numb)) {
return delete this.map[numb];
}
return false;
},
removeAll: function () {
//清空HashMap所有元素
this.map = {
};
this.arrayLink = [];
},
keySet: function () {
//获取Map中所有KEY的数组(Array)
return this.arrayLink;
}
};
HashMap.prototype.constructor = HashMap;
//使用
var ttt=new HashMap()
ttt.put(1,"3333")
ttt.put(888,"232")
//将生成html渲染进html
var asddddddd=ttt.keySet()
for(var i in asddddddd) {
var tmp= ttt.get(asddddddd[i]);
console.log(tmp)
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/190190.html原文链接:https://javaforall.cn