/**//*
name:    Map.js
author:  WindDC
date:    2006-10-27
content: 本程序用JS实现类拟JAVA中MAP类的功能
*/

function Node(key,value){//键值对对象
    this.key=key;
    this.value=value;
}

function Map(){//Map类
    this.nodes=new Array();
}

Map.prototype.put=function(key,value){//往容器中加入一个键值对
        for(var i=0;i<this.nodes.length;i++)
           if(this.nodes[i].key==key){//如果键值已存在，则put方法为更新已有数据
               this.nodes[i].value=value;
               return;
           }
        var node=new Node(key,value);
        this.nodes.push(node);
        return;
}//put

   
Map.prototype.get=function(key){//获取指定键的值
        for(var i=0;i<this.nodes.length;i++)
           if(this.nodes[i].key==key)
              return this.nodes[i].value;
        return null;
}//get
     
Map.prototype.size=function(){//获取容器中对象的个数
     return this.nodes.length;
}//size

         
Map.prototype.clear=function(){//清空容器
     while(this.nodes.length>0)
        this.nodes.pop();      
}//clear
 
Map.prototype.remove=function(key){//删除指定值
     for(var i=0;i<this.nodes.length;i++)
        if(this.nodes[i].key==key){
           this.removeByIndex(i);
			break;
        }
}//remove

Map.prototype.removeByIndex=function(index){//删除指定值
    var nodes1=new Array();
	for(var i=0;i<this.nodes.length;i++){
		if(i==index)continue;
		nodes1.push(this.nodes[i<index?i:i+1]);
	}
	this.nodes=nodes1;
}//remove
    
Map.prototype.isEmpty=function(){//是否为空
     if(this.nodes.length==0)
       return true;
     else
       return false;
}//isEmpty
    
Map.prototype.toString=function(){
     var str="[";
     for(var i=0;i<this.nodes.length;i++){
        if(i<this.nodes.length-1)
           str=str+this.nodes[i].key+",";
       else
           str=str+this.nodes[i].key;    
    }
    str=str+"]";
    return str;
} 
