js map集合的封装

[color=blue]
Map = function(){
this._array = new Array();
}

MapObj = function(k,v){
this.K = k;
this.V = v;[b][/b]

this.getK = function(){
return this.K;
}

this.getV = function(){
return this.V;
}

this.toString = function(){
return "[" + this.K + "," + this.V + "]";
}
}

Map.prototype.indexOf = function(K) {
for(var i=0;i<this.size();i++){
if(this._array[i].getK() == K){
return i;
}
}
return -1;
}

Map.prototype.put = function(K,V){ // put Method
var index = this.indexOf(K);
index != -1 ? this._array[index].V = V : this._array.push(new MapObj(K,V));
}

Map.prototype.get = function(K){ // get Method
var index = this.indexOf(K);
return (index != -1 ? this._array[index].getV() : null);
}

Map.prototype.keySet = function(){ // keySet Method
var keys = new Array();
for(var i=0;i<this.size();i++){
var key = this._array[i].getK();
keys.push(key);
}
return keys;
}

Map.prototype.values = function(){ // values Method
var values = new Array();
for(var i=0;i<this.size();i++){
var value = this._array[i].getV();
values.push(value);
}
return values;
}

Map.prototype.size = function(){ //size Method
return this._array.length;
}

Map.prototype.remove = function(K){ //remove Method
var index = this.indexOf(K);
if(index != -1){
for(var i=index;i<this.size();i++){
this._array[i] = this._array[i+1];
}
this._array.length --;
}
}

Map.prototype.isEmpty = function(){ // isEmpty Method
return this.size() == 0;
}

Map.prototype.entrySet = function(){ // entrySet Method
return this._array;
}

Map.prototype.containsKey = function(K){ // containsKey Method
return (this.indexOf(K) != -1 ? true : false);
}

Map.prototype.containsValues = function(V){ //containsValues Method
for(var i=0;i<this.size();i++){
if(this._array[i].getV() == V){
return true;
}
}
return false;
}

Map.prototype.clear = function(){ //clear Method
for(var i=0;i<this.size();i++){
this._array[i] = null;
}
this._array.length = 0;
}
[/color]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值