複製代碼代碼如下:
/**
*
*描述:JS實現的地圖方法
* @returns {map}
*/
功能映射(){
var struct =函數(鍵,值){
this.key = key;
this.value = value;
};
//添加地圖鍵值對
var put =函數(鍵,值){
for(var i = 0; i <this.arr.length; i ++){
if(this.arr [i] .key ===鍵){
this.arr [i] .value = value;
返回;
}
};
this.arr [this.arr.length] = new struct(key,value);
};
//根據鍵獲取值
var get = function(key){
for(var i = 0; i <this.arr.length; i ++){
if(this.arr [i] .key ===鍵){
返回this.arr [i] .value;
}
}
返回null;
};
//根據鍵刪除
var remove =函數(鍵){
var v;
for(var i = 0; i <this.arr.length; i ++){
v = this.arr.pop();
如果(v.key ===鍵){
繼續;
}
this.arr.unshift(v);
}
};
//獲取地圖鍵值對個數
var size = function(){
返回this.arr.length;
};
//判斷地圖是否為空
var isempty = function(){
返回this.arr.length <= 0;
};
this.arr = new array();
this.get = get;
this.put = put;
this.remove =刪除;
this.size = size;
this.isempty = isempty;
}
使用方法和java中地圖類同
複製代碼代碼如下:
<腳本類型=“ text/javascript”>
var map = new Map();
map.put(“ num”,1);
map.put(“ SS”,“ WSS”);
警報(map.size());
警報(map.get(“ num”));
paraarr.remove(“ num”);
警報(map.size());
警報(map.get(“ num”));
</script>